currently the code below will copy two spreadsheets into the macro sheet.
Problem: I want to use Excel cells to specify a file path (from cell A1, A2 or wherever), a sheet name (from cell B1, B2), and a corresponding specified cell range (in cells C1, C2) instead of having to browse to each file with the Application.
Option Explicit
Sub Sample()
Dim wb1 As Workbook: Set wb1 = ThisWorkbook
Dim wb2 As Workbook
Dim i As Long
Dim wsNew As Worksheet
Dim ws As Worksheet: Set ws = wb1.Sheets("Sheet1")
Dim LastRow
Dim sheetName As String
Dim rangeStart As String
Dim rangeEnd As String
Dim ws2 As Worksheet
Dim CellValueToCopy As String
'declare and set your worksheet with your filenames
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data by finding the last item in Column A
For i = 2 To LastRow 'loop from Row 2 to Last in Sheet1 of this workbook
Set wb2 = Workbooks.Open(ws.Cells(i, "A")) 'open the file stored in Column A of Sheet1 of this workbook
sheetName = ws.Cells(i, "B")
rangeStart = ws.Cells(i, "C")
rangeEnd = ws.Cells(i, "D")
'wb2.Sheets(ws.Cells(i, "B").Value).range(ws.Cells(i, "C").Value).Copy
Set ws2 = wb2.Worksheets(sheetName)
wb1.Sheets.Add
wb1.ActiveSheet.Name = sheetName + "_added"
' the below is a proof of concept to copy the values
' loop through the range rather than just one cell to get the final copy
CellValueToCopy = ws2.Cells(1, 1)
wb1.ActiveSheet.Cells(1, 1) = CellValueToCopy
' close workbook and reset variables
wb2.Close SaveChanges:=False
Set wb2 = Nothing
Set wsNew = Nothing
Set ws2 = Nothing
Next i
End Sub
How about something like the following, this will loop through your column A, open the given filename, and copy the Range from Column C from the Sheet in Column B and paste into a new sheet in the current workbook:
Option Explicit
Sub Sample()
Dim wb1 As Workbook: Set wb1 = Workbooks("Change from interface to Cell specify range.xlsm")
Dim wb2 As Workbook
Dim i As Long, LastRow As Long
Dim wsNew As Worksheet
Dim ws As Worksheet: Set ws = wb1.Sheets("Sheet1")
'declare and set your worksheet with your filenames, amend as required
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A
For i = 2 To LastRow 'loop from Row 2 to Last in Sheet1 of this workbook
Set wb2 = Workbooks.Open(ws.Cells(i, "A")) 'open the file stored in Column A of Sheet1 of this workbook
wb2.Sheets(ws.Cells(i, "B").Value).Range(ws.Cells(i, "C").Value).Copy
'above specify the sheet from Column B of Sheet1 and the Range from Column C
'if you have starting range at Column C and end range at Column D then the line below will copy the specified range
'wb2.Sheets(ws.Cells(i, "B").Value).Range(ws.Cells(i, "C").Value & ":" & ws.Cells(i, "D").Value).Copy
Set wsNew = wb1.Sheets.Add(After:=wb1.Sheets(wb1.Sheets.Count))
wsNew.Name = "Blah Blah " & (i - 1)
'above add a new sheet and name accordingly, I used the counter i to number the sheets
wsNew.Range("A1").PasteSpecial xlPasteAll
wb2.Close SaveChanges:=False
Set wb2 = Nothing
Set wsNew = Nothing
Next i
End Sub
Related
i have two workbooks- workbook 1 have own datas- and workbook2 have alot of data- the range that have data in workbook2 may change during days,it is not constant . i wanna copy non empty data from (A1:last row last cloumn that have content) in wokrbook2 to same range in workbook1- i wanna empty cells not to be copied from workbook2 to workbook1.
Sub Copy()
Dim ws As Worksheet
Dim ws2 As Worksheet
Dim lrow As Long
Dim i As Long
Set ws = ActiveWorkbook.Worksheets("Sheet2") 'Set the name of sheet2
Set ws2 = ActiveWorkbook.Worksheets("Sheet1") 'Set the name of sheet1
lrow = ws.Cells(Rows.Count, 1).End(xlUp).Row 'Find last row in Sheet1 'Find last row in column A, for sheet 1
For i = 1 To lrow
If Not IsEmpty(ws.Cells(i, "A").Value) Then ws2.Cells(i, "A").Value = ws.Cells(i, "A").Value
Next i
End Sub
I have numbers in the Range G2:G10, I have to check if these numbers are in one of the Cells in the Row B of the second file. Now I just have a true if argument when the same number is in G2(File 1) and B2(File 2). But how can I do this, so that when G2(File 1) and B4(File 2) are the same the if also works?
Dim cell As Range
Dim wb1 As Workbook, ws1 As Worksheet
Dim wb2 As Workbook, ws2 As Worksheet
Set wb1 = Application.Workbooks.Open("T:\folder\Map2.xlsm")
Set ws1 = wb1.Sheets("Tabelle1")
Set wb2 = Application.Workbooks.Open("T:\folder\file.xlsx")
Set ws2 = wb2.Sheets("sheet1")
For Each cell In wb1.Sheets(1).Range("G2:G10")
If cell.Value = ws2.Cells(cell.Row, "B").Value Then
ws2.Cells(cell.Row, "D").Resize(1, 3).Select
End If
Next cell
End Sub
Try this
Sub test()
Dim c As Range, cx As Range, str$
Dim wb1 As Workbook, ws1 As Worksheet
Dim wb2 As Workbook, ws2 As Worksheet
Set wb1 = Application.Workbooks.Open("T:\folder\Map2.xlsm")
Set ws1 = wb1.Sheets("Tabelle1")
Set wb2 = Application.Workbooks.Open("T:\folder\file.xlsx")
Set ws2 = wb2.Sheets("sheet1")
For Each c In ws1.Range(ws1.Cells(1, 7), ws1.Cells(ws1.Rows.Count, 7).End(xlUp))
For Each cx In ws2.Range(ws2.Cells(1, 2), ws2.Cells(ws2.Rows.Count, 2).End(xlUp))
If c = cx Then
cx.Offset(, 2).Resize(1, 3).Select
str = str & ", " & cx.Address
'Msgbox cx.Address
End If
Next cx
Next c
Msgbox "The following cells meet the conditions: " & Replace(str, ",", "", 1, 1)
End Sub
This uses a dictionary and does what I think you are looking for. Though I might have your sheets backwards. I tested using a single workbook and just added in your workbook and sheet values. I am also unsure what you want to do when a value is found so I left that blank.
Sub compare()
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim cell As Range
Dim lastrow As Long
Dim dict As Object
Set wb1 = Application.Workbooks.Open("T:\folder\Map2.xlsm")
Set ws1 = wb1.Sheets("Tabelle1")
Set wb2 = Application.Workbooks.Open("T:\folder\file.xlsx")
Set ws2 = wb2.Sheets("sheet1")
Set dict = CreateObject("Scripting.Dictionary") 'This is late bound you can change to early binding if you want
With ws2
lastrow = .Cells(.Rows.Count, 2).End(xlUp).Row
For Each cell In .Range("B1:B" & lastrow)
If Not dict.exists(cell.Value) Then 'Avoid errors
dict.Add cell.Value,cell 'Add key value, item will be the range
End If
Next cell
End With
With ws1
For Each cell In Range("G2:G10")
If dict.exists(cell.Value) Then 'Duplicate found when true
'Here we take the matched range offset and place it in the new offset range
Range(cell.Offset(0, 2), cell.Offset(0, 4)).Value = Range(dict(cell.Value).Offset(0, 2), dict(cell.Value).Offset(0, 4)).Value
End If
Next cell
End With
End Sub
I tried altering some code I have which copied all worksheets and pasted them into a new workbook into a new sheet.
Problem is I only want specific areas which all start at A5 but the last row and column is dynamic, and constantly changing.
Ideally I would like to copy each range into a new sheet in the batch rater, and keep the source sheet name only difference being I want to copy the range into A1 of the new sheets.
Sub CopyWorkbook()
Dim sh As Worksheet, wb As Workbook, sb As Workbook
Dim lRow As Long
Dim lCol As Long
Dim curName As String
Dim copy_value As Range
' get reference to both workbooks
Set sb = Workbooks("Current VA Rating Structure_test")
Set wb = Workbooks("rater_tables")
' loop through worksheets in "Rating Structure" workbook
For Each sh In sb.Worksheets
' get the max row & col of the worksheet, as well as its name
lRow = sh.Cells(sh.Rows.Count, 1).End(xlUp).Row
lCol = sh.Cells(lRow, 1).End(xlToRight).Column
curName = sh.Name
' add a worksheet to the "Rate Tables" workbook and set its name
wb.Worksheets.Add(After:=Worksheets(1)).Name = curName
' copy the range from the old sheet to the new one
sh.Range(sh.Cells(5, 1), sh.Cells(lRow, lCol)).Copy wb.Worksheets(curName).Range("A1")
Next sh
End Sub
Is there a way to copy multiple selected cells from excel as shown below? It always copies the whole range from the first selected cell to the last cell, rather than copying the values from selected cells.
A VBA code will be useful.
The following will help, this will copy the specified ranges to the clipboard so you can paste them into Notepad:
Sub CopyToClipboard()
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1")
'declare and set the worksheet you are working with, amend as required
ws.Range("B11:B12,B14,B18,B20,B22").Copy
'copy range to clipboard
End Sub
UPDATE:
A possible workaround for this would be to use a Temp worksheet and add the selected values into it and then copy that range into the Clipboard, a little long winded, but it would work:
Sub CopyToClipboard()
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1")
'declare and set the worksheet you are working with, amend as required
Dim work As Worksheet
Dim arr() As Variant
i = 0
For Each work In ThisWorkbook.Worksheets
If work.Name = "Temp" Then
Application.DisplayAlerts = False
work.Delete
Application.DisplayAlerts = True
End If
Next
'if Temp worksheet exists then delete it
For Each c In Selection
i = i + 1
ReDim Preserve arr(1 To i)
arr(i) = c.Value
Next
'above add the values from selection to an array
Set ws2 = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
ws2.Name = "Temp"
'add a temporary worksheet
For x = LBound(arr) To UBound(arr)
ws2.Cells(x, 1).Value = arr(x)
Next x
'copy values from array into temp worksheet
LastRow = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
ws2.Range("A1:A" & LastRow).Copy
'copy continuous range from Temp worksheet
End Sub
I have two separate Excel files. In one of these in Sheet1 is stored infomration about orders and order numbers. Now every time I make a new order I want this information be collected from my order and inserted in to so called "database" workbook. It should identify the last empty row in column A:A in C:\Users\user\Desktop\Order_number.xlsx and insert new values from range ("C6,C17,C10,H18,B32,G32,H6,H9") to the next empty row. Here is the code I came up to but there is some mistake and it is not working. How it can be fixed?
Sub TransferValues465()
Dim wsMain As Worksheet: Set wsMain = ThisWorkbook.ActiveSheet
Dim wsData As Worksheet: Set wsData = Workbooks.Open("C:\Users\user\Desktop\Order_number.xlsx").Sheets("Sheet1")
Dim rngToCopy As Range: Set rngToCopy = wsMain.Range("C6,C17,C10,H18,B32,G32,H6,H9")
Dim c As Long
Dim ar As Range
Dim cl As Range
Dim LastRow As Long
Dim rngDestination As Range
With Application
.DisplayAlerts = False
.ScreenUpdating = False
.EnableEvents = False
End With
'Get the last row in Database sheet:
LastRow = wsData.Cells(wsData.Rows.Count, "A").End(xlUp).Row
Set rngDestination = wsData.Cells(LastRow + 1, 1).Resize(1, 25).Offset(0, 0)
For Each ar In rngToCopy.Areas
For Each cl In ar
c = c + 1
'I used this next line for testing:
' rngDestination.Cells(c).Value = cl.Address
rngDestination.Cells(c).Value = cl.Value
Next
Next
End Sub
A few corrections:
1) Set wsData = Workbooks("C:\Users\user\Desktop\Order_number.xlsx").Sheets("Sheet1") will not work. Either use Set wsData = Workbooks("Order_number.xlsx").Sheets("Sheet1") if the workbook is open. Or you need to open the workbook first.
2) I am not famliar on using Application.WorksheetFunction.CountA(wsData.Range("A:A")) to get the last row. To get the last row in Column A (with the possibility of skipping balnk cells in the middle) use wsData.Cells(wsData.Rows.Count, "A").End(xlUp).Row.
3) My preference is to use Copy >> PasteSpecial xlPasteValues with cl.Copy and the following line wsData.Range("A" & C).PasteSpecial xlPasteValues.
Code
Option Explicit
Sub TransferValues465()
Dim wsMain As Worksheet
Dim wbData As Workbook
Dim wsData As Worksheet
Dim rngToCopy As Range
Dim C As Long
Dim ar As Range
Dim cl As Range
Dim LastRow As Long
Dim rngDestination As Range
Set wsMain = ThisWorkbook.ActiveSheet
Application.DisplayAlerts = False
' you need to open the workbook
Set wbData = Workbooks.Open("C:\Users\user\Desktop\Order_number.xlsx")
Set wsData = wbData.Sheets("Sheet1")
Set rngToCopy = wsMain.Range("C6,C17,C10,H18,B32,G32,H6,H9")
'Get the last row in Database sheet:
LastRow = wsData.Cells(wsData.Rows.Count, "A").End(xlUp).Row
C = 1
For Each cl In rngToCopy
cl.Copy
wsData.Cells(LastRow + 1, C).PasteSpecial xlPasteValues
C = C + 1
Next cl
wbData.Close True '<-- close and save the changes made
Application.DisplayAlerts = True '<-- restore settings
End Sub