I am trying to build a vba code. Some bits are working some not. Working: It is collecting valued cells from Customers workbook and pasting in new sheet in supplier workbook. New sheet is renamed on cell name. Not working: I also want to copy a header which is in Supplier workbook named as template. The last bit i want VBA code run through each column.
Sub Copy_Past_Repeat()
Dim rs As Worksheet
Dim rng As Range
Windows("Customer.xlsm").Activate
Set rng = Range("n1:n1000") 'column
rng.SpecialCells(xlCellTypeConstants).Select 'Selecting only hardcoded data
Selection.Copy
Windows("Supplier.xlsm").Activate
Sheets.Add after:=ActiveSheet
Range("C2").Select
ActiveSheet.Paste
ActiveSheet.Name = ActiveSheet.Range("C2")
'not working
ThisWorkbook.Sheets("Template").Range("A1:E1").Select
Selection.Copy
ActiveSheet.Paste 'should be pasted in just crated spreadsheet Name=(C2)
Application.CutCopyMode = False
End Sub `
It is highly recommended to avoid using .Select, .Activate or similar actions.
See How to avoid using Select in Excel VBA
Also always specify in which worksheet or workbook a range is. Otherwise Excel cannot know it and guesses.
Option Explicit
Sub Copy_Past_Repeat()
Dim rng As Range
Set rng = Workbooks("Customer.xlsm").Range("N1:N1000") 'column
rng.SpecialCells(xlCellTypeConstants).Copy 'Copy only hardcoded data
Dim NewWs As Worksheet
With Workbooks("Supplier.xlsm")
Set NewWs = .Sheets.Add(After:=.ActiveSheet) 'remember the new added sheet in NewWs so we can access it later
NewWs.Range("C2").Paste
NewWs.Name = NewWs.Range("C2")
ThisWorkbook.Worksheets("Template").Range("A1:E1").Copy
NewWs.Paste 'should be pasted in just crated spreadsheet Name=(C2)
'here you should specify where in the sheet to paste
'NewWs.Range("A1").Paste
End With
Application.CutCopyMode = False
End Sub
Related
I need to update my main file every time a third party sends me an updated version of his input.
Therefore, I need to copy-paste the range of this new input in a saved workbook on my computer. The range needs to include all columns and all rows if the value in column A is greater than 0.For example, in the picture below, from A1 to A45.
I found a way to select the rows and stop at the first zero. I've put a sumprodcut formula on the side that I call in my code i.
For now, I have this code:
I have an error on line wb1.Sheets("Accounts_latest").Range("A1:BW & i").Copy, I can't fix it... do you have any idea?
Let me know :)
Antoine
Sub CopyPaste()
Dim wb1 As Workbook
Dim wb2 As Workbook
'Open Workbook from Pepper
Set wb1 = Workbooks.Open("G:\Shared drives\Reporting\Power BI Source Files- DO NOT TOUCH\Pepper Automation\Accounts latest\Accounts updated\Accounts_latest.xlsx")
'Copy Range (Column A to BW - all filled rows)
Dim i As Integer
i = Sheets("Accounts_latest").Range("CA1").Value
wb1.Sheets("Accounts_latest").Range("A1:BW & i").Copy
'Paste to worksheet in workbook2:
Set wb2 = Workbooks("20200403 Selina - Loanbook V2.09 (1).xls")
wb2.Activate
wb2.Sheets("Pepper Accounts RAW").Range("A1:BW").PasteSpecial Paste:=xlPasteValues
Range("A1").Select
'Close workbook
wb1.Close savechanges:=True
Application.DisplayAlerts = True
End Sub
try this:
wb1.Sheets("Accounts_latest").Range("A1:BW" & i).Copy
or
wb1.Sheets("Accounts_latest").Range("A1","BW" & i).Copy
And
wb2.Sheets("Pepper Accounts RAW").Range("A1").PasteSpecial Paste:=xlPasteValues
Or
wb2.Sheets("Pepper Accounts RAW").Range("A1:BW" & i).PasteSpecial Paste:=xlPasteValues
or
wb2.Sheets("Pepper Accounts RAW").Range("A1").Resize(i,1).PasteSpecial Paste:=xlPasteValues
I have an Export to sheet button but I can't get it working correctly.
I selects the correct cells to copy but can't then transpose them on to selected sheet that appears in the drop down box in cell A1, I then also need it to paste on the next available row in that specific sheet. The problem is that I can't just list the sheets in VBA as the list in the drop down box changes. I have tried several ways to with no success. If someone could help it would be great
Sub Button2_Click()
Worksheets("Sheet1").Range("a2:x2").Copy
ActiveSheet.Paste Destination:=Worksheets("Sheet1!A1").Range("a:x")
End Sub
Here is some more code I have tried for the issue but still does not seem to work.
Sub ExportButton1()
'
' ExportButton1 Macro
' Exports Data to staff sheet from drop down box
'
' Keyboard Shortcut: Ctrl+e
'
ActiveWorkbook.Save
End Sub
Private Sub CommandButton1_Click(ByVal Target As Range)
Application.ScreenUpdating = False
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet
Set copySheet = Worksheets("Data")
'On Error Resume Next
'If Not (Application.Intersect(Range("H2"), Target) Is Nothing) Then _
Set pasteSheet = Worksheets(ActiveSheet.Range("H2"))
copySheet.Range("G5:AA5").Copy
pasteSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
you need to change the sheet name in your worksheet function.
This might work.
Sub Button2_Click()
'Copying the Data
Worksheets("Sheet1").Range("a2:x2").Copy
'Pasting the data
' What we were missing was to pass the name of the tab dynamically. Now this code will pick up the name that appears in the Cell A1.
ActiveSheet.Paste Destination:=Worksheets(Worksheets("Sheet1").Range("A1").value).Range("A1")
End Sub
Also in the paste range you only need to put the first cell range to paste values.
To paste the Transposed Values check the function PasteSpecial with Transpose property set to True.
'I have found another way around the problem to copy paste cells to certain sheet then on 'staff sheet a formula in a table to tests column A value on data sheet for name and only 'transpose those rows
Sub Macro1()
Range("A2:J2").Select
Selection.Copy
Sheets("Sheet3").Select
Range("A60000").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet1").Select
End Sub
'Formula on staff sheet is -=IF(Sheet3!A:A="Column1",Sheet3!$B:$B,"")
So I have a workbook with a master sheet column f has my citys.
I need to copy a unique list of citys from the master sheet to each existing sheet.
I've looked at :
sheets("MasterSheet").Select
Columns("F:F").Select
Selection.Copy
sheets("Existingsheet").Select
Columns("F:F").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("F1:F3125").AdvancedFilter Action:=xlFilterInPlace, Unique:=True
But I'm missing a loop as well as grabbing the existing sheet names dynamically.
I'm thinking there must be a quicker way to get to where I want to be.
Any direction would be most welcome.
Thanks.
Here is a solution that provides the loop you need to move through each worksheet and copy the unique cities to column F for each worksheet (not named "MasterSheet"). It will leave you with just the unique list and not a unique filtered list on each sheet as well as show the entire data on the MasterSheet as it was before you started.
This code assumes the city list in the MasterSheet is a contiguous list of cities starting in cell F1
Sub CopyUniqueCityList()
Dim rCityList As Range
With Sheets("MasterSheet")
With .Range(.Range("F1"), .Range("F1").End(xlDown))
.AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Set rCityList = .SpecialCells(xlCellTypeVisible)
End With
.ShowAllData
End With
Dim ws As Worksheet
For Each ws In Worksheets
If ws.Visible = xlSheetVisible And ws.Name <> "MasterSheet" Then
rCityList.Copy ws.Range("F1")
End If
Next
End Sub
You can shorten your code a little by avoiding using .Select and loop through each worksheet.
Sub test()
Dim ws as worksheet, mainWS as worksheet
Dim cityNames as Range
Set mainWS = Sheets("MasterSheet")
cityNames = mainWS.Range("F:F")
'Let's now loop through each worksheet in your activebook
For each ws in ActiveWorkbook.Worksheets
With ws
If ws.Name <> "additional" or ws.Name <> "MasterSheet" Then
.Range("F:F").Value = cityNames.Value
.Range("F1:F3125").AdvancedFilter Action:=xlFilterInPlace, Unique:=True
End if
End with
Next ws
End sub
This is untested, but should work. Please let me know if there are any errors, or it doesn't quite work.
You don't need the name of each worksheet, since you can use a For loop. Do you see how that works in the above?
I have been searching online to find a way to do this but have had no luck yet.
Am looking for a way to save my Excel file as an xlsx and not an xlsm. Only because I can not get the file to work on my Android tab as an xlsm.
The only macro I am using is this:
Sub CommandButton1_Click()
Application.ScreenUpdating = False
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet
Set copySheet = Worksheets("Sheet1")
Set pasteSheet = Worksheets("Sheet1")
copySheet.Range("a14:j14").Copy
pasteSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
It just copies a row and pastes it in the last blank row.
Id there a way to accomplish this without the use of VBA, so that the file can be saved as an xlsx and be accessed on an android tab?
Thank you for your time
I would like to call raw data from another sheet in different workbook (a column from the sheet) to fill up the column in the main sheet but both column sizes are different. I would like to use button in the main sheet whereby when I click the button, the data will be called automatically. Can this be done?
This should work for starters..
Sub temp()
Dim wb1, wb2 As Workbook
Set wb1 = ThisWorkbook
Set wb2 = Workbooks.Open("file path")
wb2.Sheets("Sheet1").Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row).Copy
wb1.Activate
wb1.Sheets("Sheet1").Select
Range("B1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub