So there is a workbook that runs a macro that was made by another group in the company and its GARBAGE but they locked it and wont let me modify the VBA. So I am trying to pull the output from that workbook into mine. The headers of the range are in merged rows with the first 11 rows being taken up by the buttons to run the macro.
Here is my current code
Sub Pull_Data()
Dim returnValue As Variant
Dim wb As Workbook
Dim ws As Worksheet
Dim wb2 As Workbook
Dim ws2 As Worksheet
Set wb2 = Application.Workbooks("name.xlsm")
Set ws2 = wb2.Worksheets("INPUT")
Dim rng As Range
r = Range("H65536").End(xlUp).Row
Set ws = wb.Worksheets("Report")
ws.Range("A11:A13").Select
Set rng = ws.Range(Cells(11, 1), Cells(r, 8))
rng.Select
rng.Copy
wb2.Activate
ws.Range(Cells(1, 1), Cells(r, 8)).PasteSpecial xlPasteAll
End Sub
My issue is that when i try to select the entire output it sometimes selects what i want (A11 to H then the last row with value) but sometimes it selects A1:H11 and its frustrating as hell to figure out the issue. If there is a better way I would love to know.
EDIT:
Updated code
Sub Pull_Data()
Dim returnValue As Variant
Dim wb As Workbook
Dim ws As Worksheet
Dim wb2 As Workbook
Dim ws2 As Worksheet
Set wb2 = Application.Workbooks("name.xlsm")
Set ws2 = wb2.Worksheets("INPUT")
Dim rng As Range
r = Application.Max(Range("H65536").End(xlUp).Row, 11)
Set ws = wb.Worksheets("Report")
ws.Range("A11:A13").Select
Set rng = ws.Range(Cells(11, 1), Cells(r, 8))
rng.Select
rng.Copy
wb2.Activate
ws.Range(Cells(1, 1), Cells(r, 8)).PasteSpecial xlPasteAll
End Sub
r = Application.Max(Range("H65536").End(xlUp).Row, 11) 'wich sheet is the range assiciated with ?
Set ws = wb.Worksheets("Report")
ws.Range("A11:A13").Select 'how is this line usefull at all ?
Set rng = ws.Range(Cells(11, 1), Cells(r, 8)) ' change to ws.Range(ws.Cells(11, 1), ws.Cells(r, 8))
rng.Select 'delete this line
rng.Copy '
wb2.Activate
ws.Range(Cells(1, 1), Cells(r, 8)).PasteSpecial xlPasteAll 'change to ws.Range(ws.Cells(1, 1), ws.Cells(r, 8)).PasteSpecial xlPasteAll
Rather than pulling data from garbage.xlsm into your workbook, copy the entire worksheet into your workbook.
Then in your version of the worksheet, remove the merging, etc.
Related
I want to copy from one sheet into another. The macro should recognize the worksheet via name:
Dim Wb1 As Workbook, wb2 As Workbook, wB As Workbook
Dim rngToCopy As Range
Dim NewEnd As Long
Dim NewEnd2 As Long
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Sheets("CALC").Select
Worksheets("CALC").Range("B5:J25000").ClearContents
For Each wB In Application.Workbooks
If Left(wB.Name, 4) = "15B2" Then
Set Wb1 = wB
Exit For
End If
Next
If Not Wb1 Is Nothing Then
Set wb2 = ThisWorkbook
With Wb1.Sheets("Data")
Set rngToCopy = .Range("F7, H7, N7", .Cells(.rows.Count, "F").End(xlUp))
End With
wb2.Sheets("CALC").Range("B5:D5").Resize(rngToCopy.rows.Count).Value = rngToCopy.Value
End If
This line gives me an error:
Set rngToCopy = .Range("F7, H7, N7", .Cells(.Rows.Count, "F").End(xlUp))
How can I copy mutiple columns in this case?
You can use Union to merge multiple columns to 1 Range.
Dim LastRow As Long
LastRow = .Cells(.Rows.Count, "F").End(xlUp).Row ' get last row with data from column "F"
Set rngToCopy = Application.Union(.Range("F7:F" & LastRow), .Range("H7:H" & LastRow), .Range("N7:N" & LastRow))
rngToCopy.Copy
wb2.Sheets("CALC").Range("B5").PasteSpecial xlPasteValues
I wrote a very simple macro, that opens another workbook and select range of data from this workbook.
However, I keep receiving this warning: object doesn't support this property or method
What is wrong?
Sub data()
Dim wb As Workbook
Dim ws As Worksheet
Dim filename As String
Dim lastrow As Integer
Dim lastcolumn As Integer
Dim range_to_copy As Range
'open workbook
filename = "C:\Users\mk\Desktop\sales report\Sales Report.xls"
Set wb = Workbooks.Open(filename)
Set ws = wb.Sheets("data")
lastcolumn = wb.ws.Cells(1, wb.ws.Columns.Count).End(xlToLeft).Column
lastrow = wb.ws.Cells(wb.ws.Roows.Count, 1).End(xlToLeft).Row
range_to_copy = Range("Cells(1,1):Cells(lastrow,lastcolumn)")
End sub
A number of things wrong.
Edit:
Dim lastrow As Integer
Dim lastcolumn As Integer
An Integer can store numbers up to 32,767. This won't be a problem for columns, but will throw an overflow error on the row number. It's better to use the Long data type - numbers up to 2,147,486,647.
The ws variable already references the workbook so you just need:
lastcolumn = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
lastrow = wb.ws.Cells(wb.ws.Roows.Count, 1).End(xlToLeft).Row
Rows only has one o in it.
Edit: xlToLeft looks at the right most cell and works to the left. As you're looking for rows you need to use xlUp which looks at the last cell and works up.
range_to_copy = Range("Cells(1,1):Cells(lastrow,lastcolumn)")
This is an object so you must Set it. The cells references are separated by a comma and should not be held as a string.
Sub data()
Dim wb As Workbook
Dim ws As Worksheet
Dim filename As String
Dim lastrow As Long
Dim lastcolumn As Long
Dim range_to_copy As Range
'open workbook
filename = "C:\Users\mk\Desktop\sales report\Sales Report.xls"
Set wb = Workbooks.Open(filename)
Set ws = wb.Sheets("data")
lastcolumn = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
lastrow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
Set range_to_copy = ws.Range(ws.Cells(1, 1), ws.Cells(lastrow, lastcolumn))
End Sub
Note: Every range reference is preceded by the worksheet reference. Without this it will always look at the currently active sheet so if you aren't on the data sheet the following will fail as the second cell reference will look at the activesheet.
ws.Range(ws.Cells(1, 1), Cells(lastrow, lastcolumn))
It might be worth checking out the With....End With code block.
There are several errors in your code.
Try
lastcolumn = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
Set range_to_copy = Range(ws.Cells(1, 1), ws.Cells(lastRow, lastcolumn))
Once you defined and set your ws worksheet object, you don't need to refer to the wb object anymore, since your ws worksheet object is fully quallified with Sheets("data") in wb workbook.
Now, all you need to do is use the With statement, like in the code below:
Set ws = wb.Sheets("data")
With ws
lastcolumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
Set range_to_copy = .Range(.Cells(1, 1), .Cells(lastrow, lastcolumn))
End With
I would really appreciate some help to find a correct approach to solve my issue.
I am attempting to loop through all worksheets (except for "Sheet 1" and "Output".
All the above referenced worksheets contain data from cell A2 to last column and last row. I need to copy all the looped ranges (one below the other) in cell C2 in my "Output" worksheet.
Also I have a unique number in A1 in all worksheets (except for "Sheet 1" and "Output" that needs to be copied into B2 in my "Output" worksheet. The trick is (which i am struggling with) the value in A1 needs to be copied down B2 in my "Output" worksheet by the number A2:last row in all my looped worksheets.
Below is my code thus far:
Sub EveryDayImShufflingData()
Dim ws As Worksheet
Dim PasteSheet As Worksheet
Dim Rng As Range
Dim lRow As Long
Dim lCol As Long
Dim maxRow As Integer
Dim x As String
Set PasteSheet = Worksheets("Output")
Application.ScreenUpdating = False
'Loop through worksheets except "Sheet 1" and "Output"
For Each ws In ActiveWorkbook.Worksheets
If (ws.Name <> "Sheet1") And (ws.Name <> "Output") And (ws.Visible = True) Then
'Select the Worksheet
ws.Select
'With each worksheet
With ws
'Declare variables lRow and lCol
lRow = .Cells(Rows.Count, 1).End(xlUp).Row
lCol = .Cells(2, .Columns.Count).End(xlToLeft).Column
'Set range exc. VIN
Set Rng = .Range(.Cells(2, 1), .Cells(lRow, lCol))
'Paste the range into "Output" worksheet
Rng.Copy
PasteSheet.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
x = .Cells(1, 1).Value
For i = 1 To lRow
PasteSheet.Cells(i, 2).End(xlUp).Offset(1, 0) = x
maxRow = maxRow + 1
Next
Application.CutCopyMode = False
Application.ScreenUpdating = True
End With
End If
Next ws
End Sub
Any assistance would be kindly appreciated
Try this:
Sub EveryDayImShufflingData()
Dim ws As Worksheet, copyRng As Range, lRow As Long, lCol As Long, PasteSheet As Worksheet
Set PasteSheet = Worksheets("Output")
For Each ws In ActiveWorkbook.Worksheets
If (ws.Name <> "Sheet1") And (ws.Name <> "Output") And (ws.Visible = True) Then
lRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
lCol = ws.Cells(2, ws.Columns.Count).End(xlToLeft).Column
Set copyRng = ws.Range(ws.Cells(2, 1), ws.Cells(lRow, lCol))
copyTargetCell = PasteSheet.Cells(Rows.Count, 3).End(xlUp).Row + 1
copyRng.Copy Destination:=PasteSheet.Range("C" & copyTargetCell)
Worksheets("Output").Range("B" & copyTargetCell & ":B" & (copyTargetCell + copyRng.Rows.Count - 1)) = ws.Range("A1")
End If
Next ws
End Sub
I am trying to copy a range of cells on a sheet in one workbook to the bottom of a sheet in another workbook. I keep getting "Application-defined or object-defined error" on the copy line.
Dim NewFileName As String
Dim BAHFileName As String
NewFileName = "Filename"
BAHFileName = "Other Filename"
LastRow = Sheets("All").UsedRange.Rows.Count
Workbooks(NewFileName).Sheets("All").Range(Cells(2, 1), Cells(LastRow, 15)).Copy
Windows(BAHFileName & ".xlsx").Activate
LastRow = Workbooks(BAHFileName).Sheets(1).UsedRange.Rows.Count + 1
Workbooks(BAHFileName).Sheets(1).Cells(LastRow, 1).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Talking to yourself?
Practise with this code to avoid using selects.
I am not sure of the situation of your workbooks, so you will have to adjust workbook names and sheet names accordingly.
Sub Button1_Click()
Dim WB As Workbook
Dim bk As Workbook
Dim LastRow As Long
Dim Lrow As Long
Dim rng As Range
Dim ws As Worksheet
Dim sh As Worksheet
Set WB = ThisWorkbook
Set bk = Workbooks("MyWorkbook.xlsx")
Set ws = WB.Sheets("All")
Set sh = bk.Sheets(1)
With ws
LastRow = .UsedRange.Rows.Count
Set rng = .Range(.Cells(2, 1), .Cells(LastRow, 15))
End With
With sh
Lrow = .UsedRange.Rows.Count + 1
rng.Copy .Cells(Lrow, 1)
End With
End Sub
I needed to select the sheet before copying.
Dim NewFileName As String
Dim BAHFileName As String
NewFileName = "Filename"
BAHFileName = "Other Filename"
LastRow = Sheets("All").UsedRange.Rows.Count
Sheets("All").Select
Workbooks(NewFileName).Sheets("All").Range(Cells(2, 1), Cells(LastRow, 15)).Copy
Windows(BAHFileName & ".xlsx").Activate
LastRow = Workbooks(BAHFileName).Sheets(1).UsedRange.Rows.Count + 1
Workbooks(BAHFileName).Sheets(1).Cells(LastRow, 1).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Why doesn't the second format of Range(Cells(),Cells()) usage work?
Sub start()
Dim ws As Worksheet
Dim wb As Workbook
Dim cond_rng As Range
Set ws = Application.Workbooks("ndata.xlsm").Worksheet("conditionsSheet")
' This works and selects one cell
Set cond_rng = ws.Cells(4, 1)
cond_rng.Select
'this works as expected
Set cond_rng = ws.Range("A1:B10")
cond_rng.Select
'this fails with error 1004
Set cond_rng = ws.Range(Cells(1, 1), Cells(4, 4))
cond_rng.Select
Ok, well this works:
'this works
Set cond_rng = ws.Range(ws.Cells(1, 1), ws.Cells(4, 4))
cond_rng.Select
or
With Sheets("conditionsSheet")
.Range(.Cells(1, 1), .Cells(4, 4)).Select
End With
The .Cells is important, as it won't work with just Cells.
I tested with a simple sub like the below and it works. I think your error is coming from missing "s" after "worksheet".
Set ws = Application.Workbooks("ndata.xlsm").Worksheet("conditionsSheet")
should be ...Worksheets....
sub TEST()
Dim ws As Worksheet
Dim cond_rng As Range
Set ws = Worksheets("sheet1")
Set cond_rng = ws.Range(Cells(1, 1), Cells(5, 4))
cond_rng.Value = 5
End Sub
I believe it is the range.select that is confusing people. you will usually get an error if you are trying to select a range from a non-active worksheet.
If you are trying to get values from that range in another sheet or need to add info into that range, you don't have to select it. For example: the below code will loop through each sheet and copy A1:D4(excluding PasteSheet) and paste it into PasteSheet.
For this example make sure you have a sheet named, "PasteSheet"
Sub SheetLoop()
Dim ws As Worksheet
Dim sh As Worksheet
Set ws = Sheets("PasteSheet")
For Each sh In Sheets
If sh.Name <> ws.Name Then
With sh
.Range(.Cells(1, 1), .Cells(4, 4)).Copy ws.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
End With
End If
Next sh
End Sub
You can run this code from any sheet in the workbook because you don't have to select anything.
So if for example Sheet2 A1=2 then sheet1(A1:D4) color is red else it is Blue.
Sub Button2_Click()
Dim ws As Worksheet, x
Dim sh As Worksheet, rng As Range, Crng As Range
Set ws = Sheets("Sheet1")
Set sh = Sheets("Sheet2")
Set rng = sh.Range("A1")
Set Crng = ws.Range(ws.Cells(1, 1), ws.Cells(4, 4))
x = IIf(rng = 2, vbRed, vbBlue)
Crng.Interior.Color = x
End Sub