The above picture is a simplified example of what I wish to achieve. Essentially, I need a macro that will go through column A, paste into cell C2, and then save a copy of the workbook. Thus, the end result will be "Type1.xlsm", "Type2.xlsm", etc. At the end of each macro run, the link to the external sheet is broken.
The issue that I am running into is that every time I save a copy of the master workbook, there is no way for the macro to go back to the original workbook. I need some way in which I can recursively call the macro.
Here is what I have so far:
Sub test()
For i = 1 To 5
If Not Range("B" & i).Value = "X" Then
Range("C2").Value = Range("A" & i).Value
Calculate 'updates the formula
Range("B" & i).Value = "X" 'update the check
ActiveWorkbook.SaveAs Filename:=Range("A" & i).Value
ActiveWorkbook.Close
'INSERT CODE TO BREAK THE EXTERNAL LINK
'At this point, how do I refer back to the original workbook?
End If
Next i
End Sub
This is what the output should be:
And an example of what "Type4.xlsm" should be:
I think this should be enough to do it. You can create an object variable pointing to the relevant sheet in the master file (may not be the first). The file containing the code can be referred to as ThisWorkbook.
Sub test()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets(1)
For i = 1 To 5
If Not ws.Range("B" & i).Value = "X" Then
ws.Range("C2").Value = ws.Range("A" & i).Value
Calculate 'updates the formula
ws.Range("B" & i).Value = "X" 'update the check
ActiveWorkbook.SaveAs Filename:=ws.Range("A" & i).Value
ActiveWorkbook.Close
'At this point, how do I refer back to the original workbook?
End If
Next i
End Sub
Use SaveCopyAs() method
Sub test()
For i = 1 To 5
If Not Range("B" & i).Value = "X" Then
Range("C2").Value = Range("A" & i).Value
Calculate 'updates the formula
Range("B" & i).Value = "X" 'update the check
ActiveWorkbook.SaveCopyAs Range("A" & i).Value
'At this point, your active workbook and worksheet are still the "original" ones
End If
Next i
End Sub
Related
I have a program that needs to copy select columns within the same workbook and worksheet.
The current code results in Excel crashing, so I'm not sure if it is working or not.
Is there a better way to copy the columns within the same worksheets with the same workbook?
Code:
Sub Macro1()
Dim wb1 As Workbook
'Set it to be the file location, name, and file extension of the Working File
Set wb1 = Workbooks.Open("Z:\XXX\Working File.xlsx")
MsgBox "Copying Fields within Working File"
wb1.Worksheets(1).Columns("G").Copy wb1.Worksheets(1).Columns("H").Value
wb1.Worksheets(1).Columns("J").Copy wb1.Worksheets(1).Columns("O").Value
wb1.Worksheets(1).Columns("K").Copy wb1.Worksheets(1).Columns("N").Value
wb1.Worksheets(1).Columns("M").Copy wb1.Worksheets(1).Columns("P").Value
wb1.Close SaveChanges:=True
End Sub
Try this, it sets two ranges' values equal, which will keep the data, but no formatting. It should be quicker.
Sub Macro1()
Dim wb1 As Workbook
'Set it to be the file location, name, and file extension of the Working File
Set wb1 = Workbooks.Open("Z:\XXX\Working File.xlsx")
MsgBox "Copying Fields within Working File"
With wb1.Worksheets(1)
.Columns("H").Value = .Columns("G").Value
.Columns("O").Value = .Columns("J").Value
.Columns("N").Value = .Columns("K").Value
.Columns("P").Value = .Columns("M").Value
End With
wb1.Close SaveChanges:=True
End Sub
Note you're using a whole column, so it might hang up or take a little longer. If you want, you can instead just get the last Row of each column and use that to shorten the ranges being copied.
Edit: As mentioned above, you may be better off using a smaller range. This is a little more verbose, but you should be able to follow what it's doing:
Sub Macro1()
Dim wb1 As Workbook
Dim lastRow As Long
'Set it to be the file location, name, and file extension of the Working File
Set wb1 = ActiveWorkbook
MsgBox "Copying Fields within Working File"
With wb1.Worksheets(1)
lastRow = .Cells(.Rows.Count, "G").End(xlUp).Row
.Range("H1:H" & lastRow).Value = .Range("G1:G" & lastRow).Value
lastRow = .Cells(.Rows.Count, "J").End(xlUp).Row
.Range("O1:O" & lastRow).Value = .Range("J1:J" & lastRow).Value
lastRow = .Cells(.Rows.Count, "K").End(xlUp).Row
.Range("N1:N" & lastRow).Value = .Range("K1:K" & lastRow).Value
lastRow = .Cells(.Rows.Count, "M").End(xlUp).Row
.Range("P1:P" & lastRow).Value = .Range("M1:M" & lastRow).Value
End With
wb1.Close SaveChanges:=True
End Sub
I have two files, one the file in which I want to run the macro in and another external file.
Within the file that the macro is running in (henceforth the "master" file), there is something that looks like this:
The code that I have so far is this:
Sub test()
For i = 1 To 3
If Not Range("B" & i).Value = "X" Then
Range("C2").Value = Range("A" & i).Value
Calculate 'updates the formula
Range("B" & i).Value = "X" 'update the check
Range("D2").Copy 'this is the tricky part - this is what is needed. The formula links needs to be broken so that only the values remain
Range("D2").PasteSpecial xlPasteValues
ActiveWorkbook.SaveCopyAs "C:\Users\n0269777\Desktop\" & Range("A" & i).Value & ".xlsm" 'the problem with SaveCopyAs is that the formula originally is now overwritten.
'thus I need some way to refer back to the 'master' workbook, the one where the formula has not yet been overwritten
End If
Next i
End Sub
What I want to achieve is that the macro will loop through and check to see if a workbook has been created with the names in column A. Then, it will update the value in "C2". Finally, a copy is saved -- and the formula is overwriten to its value, rather than remain a formula. This is the difficulty in that I cannot simply save a copy of the workbook -- the formula would have been overwritten after the run of the macro.
This is what happens in Type3.xlsm after running the macro. As you can see, the value in "D2" is 1, whereas it should be 3.
I have also considered this method:
Sub test2()
For i = 1 To 3
If Not Range("B" & i).Value = "X" Then
Range("C2").Value = Range("A" & i).Value
Calculate 'updates the formula
Range("B" & i).Value = "X" 'update the check
Set wboor = ActiveWorkbook
fileaddress = ActiveWorkbook.Path & "\" & ActiveWorkbook.Name
Range("D2").Copy 'this is the tricky part - this is what is needed. The formula links needs to be broken so that only the values remain
Range("D2").PasteSpecial xlPasteValues
wboor.SaveCopyAs "C:\Users\n0269777\Desktop\" & Range("A" & i).Value & ".xlsm" 'Perhaps I can save a copy first? Then close the workbook, so the formula is preserved
wboor.Close
Workbooks.Open Filename:=fileaddress 'but then, how do I call the original file, and then loop the macro to run again?
End If
Next i
End Sub
Any suggestions/help would be appreciated!
Not sure it was necessary to start a new question, but anyway try this.
Sub test()
Dim wb As Workbook, s As String, i As Long
For i = 1 To 3
If Not Range("B" & i).Value = "X" Then
Range("C2").Value = Range("A" & i).Value
Calculate 'updates the formula
Range("B" & i).Value = "X" 'update the check
s = "C:\Users\n0269777\Desktop\" & Range("A" & i).Value & ".xlsm"
ActiveWorkbook.SaveCopyAs s
Set wb = Workbooks.Open(s)
wb.Sheets(1).UsedRange.Value = wb.Sheets(1).UsedRange.Value
wb.Close True
End If
Next i
End Sub
You could save a copy of the formula, and put it back into the workbook each time. ie:
Option Explicit
Sub test()
With ThisWorkbook.ActiveSheet
Dim formulaText As String
formulaText = .Range("D2").Formula
Dim i As Long
For i = 1 To 3
If Not .Range("B" & i).Value = "X" Then
.Range("C2").Value = Range("A" & i).Value
Calculate 'updates the formula
.Range("B" & i).Value = "X" 'update the check
.Range("D2").Copy
.Range("D2").PasteSpecial xlPasteValues
ActiveWorkbook.SaveCopyAs "C:\Users\n0269777\Desktop\" & Range("A" & i).Value & ".xlsm"
.Range("D2").Formula = formulaText
End If
Next i
End With
End Sub
I want to make a link from active cell in workbook 1 which can I use it in workbook 2. I use the following code which assigned to a button:
With ActiveSheet
.Hyperlinks.Add Range("F6"), _
.Parent.FullName & "#'" & .Name & "'!" & "$A$1", TextToDisplay:="link"
End With
This code made a link with full path and I can use it in any workbook but I need some changes which I could to:
Make the active cell hyperlink not cell A1 which specified in code.
The value in the active cell become text to display arg of hyperlink function.
Thanks
PS after Vityata answere: how can i change Range("F6") to activecell adress?
In order to obtain the active cell value and address, change your code the corresponding places with the following:
ActiveCell.Address
ActiveCell.Value
I find it just to close this topic.
Sub Button36_Click()
Dim newRange As Range
Set newRange = Range(ActiveCell, ActiveCell.Offset(numRows, numCols))
With ActiveSheet
.Hyperlinks.Add Anchor:=newRange, _
Address:=.Parent.FullName & "#'" & .Name & "'!" & ActiveCell.Address, TextToDisplay:=ActiveCell.Text
End With
End Sub
try this
Sub add_links_Input_Column()
Dim lRow As Long
Dim ColHead As String
ColHead = InputBox("Enter Column Letter", "Identify Column", [c1].Value)
If ColHead = "" Then Exit Sub
With ActiveSheet
lRow = .Range(ColHead & .Rows.Count).End(xlUp).Row
For Each c In .Range(ColHead & "2:" & ColHead & lRow)
ActiveSheet.Hyperlinks.Add anchor:=c, Address:=c.Value
Next
End With
End Sub
I have an Excel macro that copies and pastes line items from a source file to an Excel-based form. It opens a form template and saves each line item as it's own file then loops through the remaining rows. Right now I have a file path built into the code that refers to the form template needed, but I need the user to be able to choose which file they want to use as their template. I have code for both of these processes but I haven't been able to combine them. My example below results in a Compile Error: Variable not defined.
Here's what I have so far:
Option Explicit
Sub CopyToForm()
Dim wbSource As Workbook, wbForm As Workbook
Dim wsSource As Worksheet, wsForm As Worksheet
Dim formpath As String, foldertosavepath As String
Dim lrow As Long, i As Integer
Set wbSource = ThisWorkbook '~~> Write your code in Indication Tool.xls
Set wsSource = wbSource.Sheets("Indication Tool") '~~> Put the source sheet name
'~~> This opens the Processing Form template.
formpath = "C:\File path.xls"
'~~> Prompts user with Open File Dialog Box
strCancel = "N"
strWorkbookNameAndPath = Application.GetOpenFilename _
(FileFilter:=strFilt, _
FilterIndex:=intFilterIndex, _
Title:=strDialogueFileTitle)
'~~> Exits If No File Selected
If strWorkbookNameAndPath = "" Then
MsgBox ("No Filename Selected")
strCancel = "Y"
Exit Sub
ElseIf strWorkbookNameAndPath = "False" Then
MsgBox ("You Clicked The Cancel Button")
strCancel = "Y"
Exit Sub
End If
Workbooks.Open strWorkbookNameAndPath
'~~> This declares path where the Individual forms will be saved.
foldertosavepath = "C:\File path\Forms\"
With wsSource
'~~> Counts how many rows are in the Indication Tool
lrow = .Range("B" & .Rows.Count).End(xlUp).Row
If lrow < 18 Then MsgBox "No data for transfer": Exit Sub
For i = 18 To lrow
Set wbForm = Workbooks.Open(formpath) '~~> open the form
Set wsForm = wbForm.Sheets("Processing Form") '~~> Declare which worksheet to activate
'~~> Proceed with the copying
.Range("B" & i).Copy wsForm.Range("F7:K7")
.Range("C" & i).Copy wsForm.Range("D8")
.Range("C" & i).Copy wsForm.Range("D30")
.Range("D" & i).Copy wsForm.Range("H29")
.Range("E" & i).Copy wsForm.Range("E29")
.Range("F" & i).Copy wsForm.Range("D33")
.Range("G" & i).Copy wsForm.Range("J30:K30")
.Range("H" & i).Copy wsForm.Range("P33")
.Range("I" & i).Copy wsForm.Range("L33:N33")
.Range("L" & i).Copy wsForm.Range("H32")
.Range("R" & i).Copy wsForm.Range("D87")
.Range("C2:F2").Copy wsForm.Range("J101:M101")
.Range("C3:M3").Copy wsForm.Range("E102:O102")
'~~> Save the form using the client name
wbForm.SaveAs foldertosavepath & .Range("B" & i).Value & ".xls"
'~~> These steps are for formatting, as I haven't figured out how paste values only
Set wbForm = Workbooks.Open(formpath)
Cells.Select
Selection.Copy
wsForm.Activate
'~~> This allows the format to be pasted into the updated Form
wsForm.Unprotect
Cells.Select
Selection.PasteSpecial Paste:=xlPasteFormats
wsForm.Protect
ActiveWorkbook.Save
ActiveWorkbook.Close
wbForm.Close ([SaveChanges:=False])
Set wbForm = Nothing
Set wsForm = Nothing
Next
End With
End Sub
When I debug the error, Sub CopyToForm() is highlighted in yellow and strCancel = is selected. Is there a way to set the user-chosen file as the formpath? Thanks in advance for your help, this has been a thorn in my side for quite some time.
Option Explicit is declared, and strCancel isn't declared as a variable.
Add Dim strCancel As String to your code
I have a strange issue whereby when I paste data from copied cells from excel in to NotePad++, it puts the data inside quotations and misses the line breaks that are in the cell.
I am creating the copy range (although I have tried manually copying one cell) form a VBA script that also adds the data to the cells.
Here is the code incase it helps:
Sub Ready_For_Infra()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim cell As Range
Dim i As Long, lastrow As Long, lastcol As Long, g As Long
Dim str1 As String
Set ws1 = Worksheets("InfraData")
Set ws2 = Worksheets("ActionPlan")
ws1.Cells.Clear
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
With ws2
lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
lastcol = .Cells(1, .Columns.Count).End(xlToLeft).Column
For i = lastrow To 2 Step -1
str1 = ""
For Each cell In .Range(.Cells(i, 6), .Cells(i, lastcol))
g = i - 1
If cell.Column <> 4 And cell.Column <> 5 And cell.Value <> "" And cell.Value <> "NEW ACTION" Then str1 = str1 & cell.Value & Chr(10) & "(" & cell.Offset(-g, 0).Value & ")" & Chr(10)
Next cell
ws1.Range("A" & 2 + lastrow - i).Value = ws2.Cells(i, 1).Value & Chr(10) & Chr(10) & ws2.Cells(i, 2).Value & Chr(10) & Chr(10) & str1
Next i
End With
ws1.Range("A2", "A" & lastrow).Copy
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
MsgBox "Done"
End Sub
The pasted data should look like this:
1
Testing1
Another Day of testing
(05/03/2014)
But instead looks like this:
"1Testing1AnotherDayoftesting(05/03/2014)"
However, when I paste it in to here, it appeared to include the line breaks and spaces but still include the quotations. (See Below)
"1
Testing1
Another Day of testing
(05/03/2014)
"
To get around leaving the quotes out when pasting from Excel to Notepad or others, I use the code below to put stuff in the clipboard:
Dim obj As New DataObject
obj.SetText NameofYourVariable
obj.PutInClipboard
It then pastes nicely without any double quotes.