Jared_Jones_23
New Member
- Joined
- Jun 24, 2011
- Messages
- 34
I have a macro that when its run it opens another workbook and selects specific data, based on conditions someone has on the original sheet, then copies the data to paste onto the original sheet. I have a code that works but when I try to reference the last cell on the sheet I am opening it finds the last row of the table which is 6600 instead of the last used row which is around 200. I will highlight where in the code the problem is, thank you for any ideas and help.
Sub Data()
Dim filenames As Variant
Dim macroname As String
Dim fnam, cse As String
Dim wbOpen As Workbook
Dim src_frow, src_lrow, tgt_frow, tgt_lrow, src_lcol, delcnt As Long
src_frow = 20
tgt_frow = 20
src_lcol = 138
cse = Range("D4").Value
delcnt = 0
macroname = "MACRO_COMBINE.xlsm"
fnam = ActiveWorkbook.Name
'If fnam <> macroname Then
' MsgBox "Please open " & macroname
' GoTo endout
'End If
Application.ScreenUpdating = False
filenames = Application.GetOpenFilename(, , , , True)
counter = 1
While counter <= UBound(filenames) ' ubound determines array size
Application.EnableEvents = False
Set wbOpen = Workbooks.Open(filenames(counter))
Application.EnableEvents = True
Sheets("Data").Select
Range("A20").Select
Selection.AutoFilter
ActiveSheet.rows.EntireRow.Hidden = False
ActiveSheet.Columns.EntireColumn.Hidden = False
If cse = "Total" Then
Call delbrows1(src_frow)
Else
Call delbrows2(src_frow, cse, delcnt)
End If
src_lrow = LastRowIndex(ActiveSheet, "B") 'LastRowIndex(ActiveSheet, 2)
Range(Cells(src_frow, 1), Cells(src_lrow, src_lcol)).Copy
Windows(macroname).Activate
tgt_lrow = src_lrow - src_frow + tgt_frow
Range(Cells(tgt_frow, 1), Cells(tgt_lrow, src_lcol)).PasteSpecial
Application.CutCopyMode = False
wbOpen.Close False
tgt_frow = tgt_lrow + 1
counter = counter + 1
cwrite = counter - 1
Wend
Call mod_cse(src_frow, cse, fname, cnt)
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs "SAM_" & cse & "_" & Format(Now, "yyyymmdd") & ".xlsx", FileFormat:=51
Application.ScreenUpdating = True
Application.DisplayAlerts = True
MsgBox ("File saved as: " & ActiveWorkbook.Name _
& vbCr & vbCr)
endout:
End Sub
Sub delbrows1(fr)
Dim r As Long
For r = Cells(rows.Count, 2).End(xlUp).Row To fr Step -1
'For r = 1000 To 1 Step -1
If Cells(r, 2) = "" Then rows(r).Delete
Next r
End Sub
Sub delbrows2(fr, cse, delcnt)
Dim r As Long
For r = Cells(rows.Count, "B").End(xlUp).Row To fr Step -1
'For r = 1000 To 1 Step -1
If Cells(r, 8) <> cse Then
rows(r).Delete
delcnt = delcnt + 1
End If
Next r
End Sub
Public Function GetLastRowWithData() As Long
Dim ExcelLastCell As Object, lRow As Long, lLastDataRow As Long, l As Long
Set ExcelLastCell = ActiveSheet.Cells.SpecialCells(xlLastCell)
lLastDataRow = ExcelLastCell.Row
lRow = ExcelLastCell.Row
Do While Application.CountA(ActiveSheet.rows(lRow)) = 0 And lRow <> 1
lRow = lRow - 1
Loop
lLastDataRow = lRow
GetLastRowWithData = lLastDataRow
End Function
Function LastRowIndex(ByVal w As Worksheet, ByVal col As Variant) As Long
LastRowIndex = .Cells(.rows.Count, "D").End(xlUp).Row
End Function
Sub mod_cse(src_frow, cse, fname, cnt)
ActiveSheet.Shapes("Button 17").Cut
'Range("a7:b18").ClearContents
'Range("c7").ClearContents
'Range("B7").Select
'With Selection.Validation
'.Delete
'End With
Range("B7").Value = cse
If cse = "Total" Then
Range("A7").Value = "PSR"
Else
Range("A7").Value = "Filtered"
End If
begin = 8
For c = 1 To cnt
myrow = begin + c
If myrow < 18 Then
Cells(myrow, 2) = fname(c)
Else
MsgBox "Too many files to document in list"
End If
Next c
Range("A20").Select
End Sub
Sub Data()
Dim filenames As Variant
Dim macroname As String
Dim fnam, cse As String
Dim wbOpen As Workbook
Dim src_frow, src_lrow, tgt_frow, tgt_lrow, src_lcol, delcnt As Long
src_frow = 20
tgt_frow = 20
src_lcol = 138
cse = Range("D4").Value
delcnt = 0
macroname = "MACRO_COMBINE.xlsm"
fnam = ActiveWorkbook.Name
'If fnam <> macroname Then
' MsgBox "Please open " & macroname
' GoTo endout
'End If
Application.ScreenUpdating = False
filenames = Application.GetOpenFilename(, , , , True)
counter = 1
While counter <= UBound(filenames) ' ubound determines array size
Application.EnableEvents = False
Set wbOpen = Workbooks.Open(filenames(counter))
Application.EnableEvents = True
Sheets("Data").Select
Range("A20").Select
Selection.AutoFilter
ActiveSheet.rows.EntireRow.Hidden = False
ActiveSheet.Columns.EntireColumn.Hidden = False
If cse = "Total" Then
Call delbrows1(src_frow)
Else
Call delbrows2(src_frow, cse, delcnt)
End If
src_lrow = LastRowIndex(ActiveSheet, "B") 'LastRowIndex(ActiveSheet, 2)
Range(Cells(src_frow, 1), Cells(src_lrow, src_lcol)).Copy
Windows(macroname).Activate
tgt_lrow = src_lrow - src_frow + tgt_frow
Range(Cells(tgt_frow, 1), Cells(tgt_lrow, src_lcol)).PasteSpecial
Application.CutCopyMode = False
wbOpen.Close False
tgt_frow = tgt_lrow + 1
counter = counter + 1
cwrite = counter - 1
Wend
Call mod_cse(src_frow, cse, fname, cnt)
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs "SAM_" & cse & "_" & Format(Now, "yyyymmdd") & ".xlsx", FileFormat:=51
Application.ScreenUpdating = True
Application.DisplayAlerts = True
MsgBox ("File saved as: " & ActiveWorkbook.Name _
& vbCr & vbCr)
endout:
End Sub
Sub delbrows1(fr)
Dim r As Long
For r = Cells(rows.Count, 2).End(xlUp).Row To fr Step -1
'For r = 1000 To 1 Step -1
If Cells(r, 2) = "" Then rows(r).Delete
Next r
End Sub
Sub delbrows2(fr, cse, delcnt)
Dim r As Long
For r = Cells(rows.Count, "B").End(xlUp).Row To fr Step -1
'For r = 1000 To 1 Step -1
If Cells(r, 8) <> cse Then
rows(r).Delete
delcnt = delcnt + 1
End If
Next r
End Sub
Public Function GetLastRowWithData() As Long
Dim ExcelLastCell As Object, lRow As Long, lLastDataRow As Long, l As Long
Set ExcelLastCell = ActiveSheet.Cells.SpecialCells(xlLastCell)
lLastDataRow = ExcelLastCell.Row
lRow = ExcelLastCell.Row
Do While Application.CountA(ActiveSheet.rows(lRow)) = 0 And lRow <> 1
lRow = lRow - 1
Loop
lLastDataRow = lRow
GetLastRowWithData = lLastDataRow
End Function
Function LastRowIndex(ByVal w As Worksheet, ByVal col As Variant) As Long
LastRowIndex = .Cells(.rows.Count, "D").End(xlUp).Row
End Function
Sub mod_cse(src_frow, cse, fname, cnt)
ActiveSheet.Shapes("Button 17").Cut
'Range("a7:b18").ClearContents
'Range("c7").ClearContents
'Range("B7").Select
'With Selection.Validation
'.Delete
'End With
Range("B7").Value = cse
If cse = "Total" Then
Range("A7").Value = "PSR"
Else
Range("A7").Value = "Filtered"
End If
begin = 8
For c = 1 To cnt
myrow = begin + c
If myrow < 18 Then
Cells(myrow, 2) = fname(c)
Else
MsgBox "Too many files to document in list"
End If
Next c
Range("A20").Select
End Sub