Hello I have developed a VBA macro for multiple sheets in a workbook that clears out previous Data, grabs a Txt file from another location then copies and pastes the data from the text file into the specific worksheet as the new data. I have gotten the macro to work for all the worksheets, however I need to be able to adjust for needing more or less rows. It has worked perfectly for me to do this on my first worksheets but when attempting to apply the same method I used to the other worksheets I get errors. Could this be an issue with how I define the worksheets?
Here is my code where the Lastrow adjustments work fine.
Here is the code for another worksheet I want to be able to adjust for the last row but have gotten errors.
Here is my code where the Lastrow adjustments work fine.
Rich (BB code):
Sub BeforenAfterRemap()
'
' BeforenAfterRemap Macro
'
LR = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
Rows(LR).Delete
Workbooks.OpenText Filename:= _
"K:\1900\dwprod1900\data\export\general\before_n_after_remap_audit_umroi.txt", _
Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 2), _
Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1)), _
TrailingMinusNumbers:=True
Windows("UMROI_Standard Cost Audit Reports.xlsm").Activate
Dim LastRowT As Long, LastRowS As Long
Dim wks1 As Worksheet
Dim wks2 As Worksheet
Set wks2 = Workbooks("UMROI_Standard Cost Audit Reports.xlsm").Worksheets("Before n After Remap Review")
Set wks1 = Workbooks("before_n_after_remap_audit_umroi.txt").Worksheets("before_n_after_remap_audit_umro")
LastRowS = Workbooks("before_n_after_remap_audit_umroi.txt").Worksheets("before_n_after_remap_audit_umro").Range("A:E").Find("*", , xlValues, xlPart, xlByRows, xlPrevious, False).Row
LastRowS = Workbooks("before_n_after_remap_audit_umroi.txt").Worksheets("before_n_after_remap_audit_umro").Range("F:G").Find("*", , xlValues, xlPart, xlByRows, xlPrevious, False).Row
LastRowT = Workbooks("UMROI_Standard Cost Audit Reports.xlsm").Worksheets("Before n After Remap Review").Range("A:E").Find("*", , xlValues, xlPart, xlByRows, xlPrevious, False).Row
LastRowT = Workbooks("UMROI_Standard Cost Audit Reports.xlsm").Worksheets("Before n After Remap Review").Range("I:J").Find("*", , xlValues, xlPart, xlByRows, xlPrevious, False).Row
Worksheets("Before n After Remap Review").Range("A7:E" & LastRowT).Select
Selection.ClearContents
Worksheets("Before n After Remap Review").Range("I7:J" & LastRowT).Select
Selection.ClearContents
Workbooks("before_n_after_remap_audit_umroi.txt").Worksheets("before_n_after_remap_audit_umro").Range("A1:E" & LastRowS).Copy
Workbooks("UMROI_Standard Cost Audit Reports.xlsm").Worksheets("Before n After Remap Review").Range("A7:E" & LastRowT).PasteSpecial
Application.CutCopyMode = False
Workbooks("before_n_after_remap_audit_umroi.txt").Worksheets("before_n_after_remap_audit_umro").Range("F1:G" & LastRowS).Copy
Workbooks("UMROI_Standard Cost Audit Reports.xlsm").Worksheets("Before n After Remap Review").Range("I7:J" & LastRowT).PasteSpecial
Application.CutCopyMode = False
LastRowT = Range("A" & Rows.Count).End(xlUp).Row
Range("F7").AutoFill Destination:=Range("F7:F" & LastRowT), Type:=xlFillDefault
LastRowT = Range("A" & Rows.Count).End(xlUp).Row
Range("G7").AutoFill Destination:=Range("G7:G" & LastRowT), Type:=xlFillDefault
LastRowT = Range("A" & Rows.Count).End(xlUp).Row
Range("K7").AutoFill Destination:=Range("K7:K" & LastRowT), Type:=xlFillDefault
LastRowT = Range("A" & Rows.Count).End(xlUp).Row
Range("L7").AutoFill Destination:=Range("L7:L" & LastRowT), Type:=xlFillDefault
LastRowT = Range("A" & Rows.Count).End(xlUp).Row
Range("N7").AutoFill Destination:=Range("N7:N" & LastRowT), Type:=xlFillDefault
With wks2
LastRowT = .Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
wks2.Range("A7:E" & LastRowT).Value = .Range("A7:E" & LastRowT).Value
wks2.Range("I7:J" & LastRowT).Value = .Range("I7:J" & LastRowT).Value
End With
With wks2
With .Cells(LastRowT + 1, 4)
.Formula = "=SUM(D7:D" & LastRowT & ")"
.Resize(, 3).FillRight
End With
.Calculate
.Parent.Activate
With .Cells(LastRowT + 1, 9)
.Formula = "=SUM(I7:I" & LastRowT & ")"
.Resize(, 3).FillRight
End With
.Calculate
.Parent.Activate
With .Cells(LastRowT + 1, 7)
.Offset(1).Formula = Replace("=IF($D@LastRowT + 1=0,IF($F@LastRowT + 1=0,0,IF($F@LastRowT + 1<>0,1,$F@LastRowT + 1/$D@LastRowT + 1)),$F@LastRowT + 1/$D@LastRowT + 1)", "@LastRowT + 1", LastRowT + 1)
.Resize(, 1).FillRight
End With
With .Cells(LastRowT + 1, 12)
.Offset(1).Formula = Replace("=IF($I@LastRowT + 1=0,IF($K@LastRowT + 1=0,0,IF($K@LastRowT + 1<>0,1,$K@LastRowT + 1/$I@LastRowT + 1)),$K@LastRowT + 1/$I@LastRowT + 1)", "@LastRowT + 1", LastRowT + 1)
.Resize(, 1).FillRight
End With
With .Cells(LastRowT + 1, 14)
.Offset(1).Formula = "=(L" & LastRowT + 2 & " - G" & LastRowT + 2 & ")"
.Resize(, 1).FillRight
End With
End With
Application.ScreenUpdating = True
Set wks1 = Nothing
Set wks2 = Nothing
Range("G" & LastRowT + 2).Select
Selection.Cut
Range("G" & LastRowT + 1).Select
ActiveSheet.Paste
Range("L" & LastRowT + 2).Select
Selection.Cut
Range("L" & LastRowT + 1).Select
ActiveSheet.Paste
Range("N" & LastRowT + 2).Select
Selection.Cut
Range("N" & LastRowT + 1).Select
ActiveSheet.Paste
Range("G" & LastRowT + 1).NumberFormat = "0.00%"
Range("L" & LastRowT + 1).NumberFormat = "0.00%"
Range("N" & LastRowT + 1).NumberFormat = "0.00%"
Range("D" & LastRowT + 1).NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)"
Range("E" & LastRowT + 1).NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)"
Range("F" & LastRowT + 1).NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)"
Range("I" & LastRowT + 1).NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)"
Range("J" & LastRowT + 1).NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)"
Range("K" & LastRowT + 1).NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)"
Range("A" & LastRowT + 1).Interior.ColorIndex = 15
Range("B" & LastRowT + 1).Interior.ColorIndex = 15
Range("C" & LastRowT + 1).Interior.ColorIndex = 15
Range("D" & LastRowT + 1).Interior.ColorIndex = 15
Range("E" & LastRowT + 1).Interior.ColorIndex = 15
Range("F" & LastRowT + 1).Interior.ColorIndex = 15
Range("G" & LastRowT + 1).Interior.ColorIndex = 35
Range("I" & LastRowT + 1).Interior.ColorIndex = 15
Range("J" & LastRowT + 1).Interior.ColorIndex = 15
Range("K" & LastRowT + 1).Interior.ColorIndex = 15
Range("L" & LastRowT + 1).Interior.ColorIndex = 35
Range("N" & LastRowT + 1).Interior.ColorIndex = 35
Range("A" & LastRowT + 1).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("B" & LastRowT + 1).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("C" & LastRowT + 1).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("D" & LastRowT + 1).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("E" & LastRowT + 1).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("F" & LastRowT + 1).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("G" & LastRowT + 1).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("I" & LastRowT + 1).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("J" & LastRowT + 1).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("K" & LastRowT + 1).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("L" & LastRowT + 1).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("N" & LastRowT + 1).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Dim ChkRange As Range
Set ChkRange = Range("A7:A" & LastRowT)
Dim cell As Range
If Application.WorksheetFunction.CountBlank(Range("A7:A" & LastRowT)) > 0 Then
Range("A7:A" & LastRowT).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End If
Windows("before_n_after_remap_audit_umroi.txt").Activate
ActiveWindow.Close
'
End Sub
Here is the code for another worksheet I want to be able to adjust for the last row but have gotten errors.
Rich (BB code):
Sub AccountToComponentAudit()
'
' AccountToComponentAudit Macro
'
'
Range("A9:F147").Select
Selection.ClearContents
Workbooks.OpenText Filename:= _
"K:\1900\dwprod1900\data\export\general\ent_component_audit_umroi.txt", Origin _
:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote _
, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:= _
False, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 2), Array(2, 1) _
, Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1)), _
TrailingMinusNumbers:=True
Range("A1:F139").Select
Selection.Copy
Windows("UMROI_Standard Cost Audit Reports.xlsm").Activate
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("ent_component_audit_umroi.txt").Activate
Application.CutCopyMode = False
ActiveWindow.Close
End Sub