R
ryguy7272
I am trying to come up with a batch processing macro that opens each excel
file in a folder, checks all cells in Column I, and if they are not blank,
inserts something like this into Cells (adjacent to the non-blank cells) in
Column J:
=IF(E2<>"",F2,IF(G2<>"",H2))
Option Explicit
Sub testme01()
Dim tempWkbk As Workbook
Dim myNames() As String
Dim fCtr As Long
Dim myFile As String
Dim myPath As String
'change to point at the folder to check
myPath = "C:\Ryan"
If Right(myPath, 1) <> "\" Then
myPath = myPath & "\"
End If
myFile = Dir(myPath & "*.xls")
If myFile = "" Then
MsgBox "no files found"
Exit Sub
End If
'get the list of files
fCtr = 0
Do While myFile <> ""
fCtr = fCtr + 1
ReDim Preserve myNames(1 To fCtr)
myNames(fCtr) = myFile
myFile = Dir()
Loop
If fCtr > 0 Then
For fCtr = LBound(myNames) To UBound(myNames)
Set tempWkbk = Workbooks.Open(Filename:=myPath & myNames(fCtr))
'your macro that does the work goes
For Each C In Range("I2:I100")
If C.Value <> "" Then
ActiveCell.Offset C.Value = "=IF(E2<>"",F2,IF(G2<>"",H2))"
Next C
tempWkbk.Close savechanges:=True
Next fCtr
End If
End Sub
I am having problems with the Loop: For Each C…Next C.
Also, I don’t necessarily want the macro to loop from I2:I100; if some of
those cells are blank I want Excel to stop working on that Worksheet and
start working on the next Workbook. There must be some syntax to cause the
macro to perform an operation only in a Used range, or only if cells are
<>â€â€. Does anyone know how to set this up?
Regards,
Ryan---
file in a folder, checks all cells in Column I, and if they are not blank,
inserts something like this into Cells (adjacent to the non-blank cells) in
Column J:
=IF(E2<>"",F2,IF(G2<>"",H2))
Option Explicit
Sub testme01()
Dim tempWkbk As Workbook
Dim myNames() As String
Dim fCtr As Long
Dim myFile As String
Dim myPath As String
'change to point at the folder to check
myPath = "C:\Ryan"
If Right(myPath, 1) <> "\" Then
myPath = myPath & "\"
End If
myFile = Dir(myPath & "*.xls")
If myFile = "" Then
MsgBox "no files found"
Exit Sub
End If
'get the list of files
fCtr = 0
Do While myFile <> ""
fCtr = fCtr + 1
ReDim Preserve myNames(1 To fCtr)
myNames(fCtr) = myFile
myFile = Dir()
Loop
If fCtr > 0 Then
For fCtr = LBound(myNames) To UBound(myNames)
Set tempWkbk = Workbooks.Open(Filename:=myPath & myNames(fCtr))
'your macro that does the work goes
For Each C In Range("I2:I100")
If C.Value <> "" Then
ActiveCell.Offset C.Value = "=IF(E2<>"",F2,IF(G2<>"",H2))"
Next C
tempWkbk.Close savechanges:=True
Next fCtr
End If
End Sub
I am having problems with the Loop: For Each C…Next C.
Also, I don’t necessarily want the macro to loop from I2:I100; if some of
those cells are blank I want Excel to stop working on that Worksheet and
start working on the next Workbook. There must be some syntax to cause the
macro to perform an operation only in a Used range, or only if cells are
<>â€â€. Does anyone know how to set this up?
Regards,
Ryan---