I've been struggling with the following code for the past weeks.
What it does is pretty simple, it allows me to retrieve data from multiple worksheets without opening them. The issue is that I have around 150 rows & 1700 columns of data to be filled thus around 255k cells of data... So it takes way too much time
The workbook looks like this:
Sub Worksheet_Change()
Dim Rng As Range
Dim r As Long
Dim s As String
Dim f As String
Dim i As Long
On Error GoTo ErrHandler
Dim m As Long
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.AskToUpdateLinks = False
Application.DisplayAlerts = False
Application.DisplayStatusBar = False
m = Sheets("ECHEANCIER").Range("A" & Rows.Count).End(xlUp).Row
Range("I5:BMQ" & m).ClearContents
For r = 5 To m
For i = 9 To 1000
s = "'" & Range("B" & r).Value & "[" & Range("C" & r).Value & "]" & Range("D" & r).Value & "'!"
Sheets("ECHEANCIER").Cells(r, i).FormulaR1C1 = "=RC[-1]*(IF(ISNA(INDEX(" & s & " R1C1:R1000C1000,MATCH(R[" & 4 - r & "]C," & s & "C1,0),MATCH(RC7," & s & " R1,0)+1)),1,INDEX(" & s & " R1C1:R1000C1000,MATCH(R[" & 4 - r & "]C," & s & "C1,0),MATCH(RC7," & s & " R1,0)+1)))"
If Sheets("ECHEANCIER").Cells(r, i).Value = 0 Then
Sheets("ECHEANCIER").Cells(r, i).ClearContents
Exit For
End If
Range("I" & r & ":BMQ" & r).Copy
Range("I" & r & ":BMQ" & r).PasteSpecial Paste:=xlPasteValues
Next
Next
ExitHandler:
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.AskToUpdateLinks = True
Application.DisplayAlerts = True
Application.DisplayStatusBar = True
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation
Resume ExitHandler
End Sub
I am also trying to having the formula already written instead of writting it VBA, but I understood that it is quite complicated and that you have to copy/paste as values in order to get the result but I don't get it.
Thanks a lot for your help!!!
Related
I am making a tool in excel VBA to bulk create some kind of invoices to each customer. We are making LIVE streams and selling kids clothing, then we write all our orders to excel sheet. Example:
orders list
Then we have to sort all those orders by customer (there are many of them) and create some kind of invoice for each customer. Example: "invoice template"
I use this code to bulk create those and download as pdfs.
Sub Create_PDF_Files()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Dim dsh As Worksheet
Dim tsh As Worksheet
Dim setting_Sh As Worksheet
Set dsh = ThisWorkbook.Sheets("uzsakymai")
Set tsh = ThisWorkbook.Sheets("lapukas")
Set setting_Sh = ThisWorkbook.Sheets("Settings")
Application.DisplayStatusBar = True
Application.StatusBar = ""
Dim i As Integer
Dim File_Name As String
For i = 2 To dsh.Range("A" & Application.Rows.Count).End(xlUp).Row
Application.StatusBar = i - 1 & "/" & dsh.Range("A" & Application.Rows.Count).End(xlUp).Row - 1
tsh.Range("D1").Value = dsh.Range("C" & i).Value
tsh.Range("A4").Value = dsh.Range("B" & i).Value
tsh.Range("B4").Value = dsh.Range("A" & i).Value & " - " & dsh.Range("E" & i).Value
tsh.Range("P4").Value = dsh.Range("D" & i).Value
File_Name = dsh.Range("A" & i).Value & "(" & dsh.Range("C" & i).Value & "-" & dsh.Range("D" & i).Value & ").pdf"
tsh.ExportAsFixedFormat xlTypePDF, setting_Sh.Range("F4").Value & "\" & File_Name
Next i
Application.StatusBar = ""
MsgBox "Done"
End Sub
But what it does is creating invoice for each item.
EXAMPLE
Any ideas how could I make it work for me as I want it to work?
---EDIT---
After ALeXceL answer, it seems to have some bugs. I changed my code to his code, and I see some progress in creating this program, but what it does, is it shows first item correctly, but all the other items are appearing starting on A24 cell. EXAMPLE
---EDIT---
IT WORKS!!!
Assuming that "uzsakymai" is "orders", the 'data sheet' (dsh) and "lapukas" is the 'template' sheet (tsh), I did these changes, added some counters, in order to the logic flows the right way:
Important: before put this code to run you MUST classify the 'orders' table (dsh, or "uzsakymai") first by Name, then, by Size (as you wish, according to the images posted)
Option Explicit
Sub Create_PDF_Files()
Dim Orders_sh As Worksheet
Dim Template_sh As Worksheet
Dim setting_Sh As Worksheet
Dim oCell As Excel.Range
Dim strKey_TheName As String
Dim lngTemplate_A As Long
Dim lngSumOfItems As Long
Dim dblSumOfValues As Double
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set Orders_sh = ThisWorkbook.Sheets("uzsakymai")
Set Template_sh = ThisWorkbook.Sheets("lapukas")
Set setting_Sh = ThisWorkbook.Sheets("Settings")
Application.DisplayStatusBar = True
Application.StatusBar = ""
Dim lngI As Long
Dim File_Name As String
'At this point, the Orders_sh worksheet should already have its fields properly sorted/ordered. (Name, then Size)
lngI = 2
Application.StatusBar = lngI - 1 & "/" & Orders_sh.Range("A11").End(xlUp).Row - 1 'a maximum of 10 items can be written here!
Set oCell = Orders_sh.Range("A" & lngI) ' the initial cell
Do
strKey_TheName = UCase(Orders_sh.Range("C" & lngI).Value)
lngSumOfItems = 0
dblSumOfValues = 0
Do
Template_sh.Range("D1").Value = Orders_sh.Range("C" & lngI).Value
lngTemplate_A = IIf(lngSumOfItems = 0, 4, Template_sh.Range("A10").End(xlUp).Offset(1, 0).Row)
Template_sh.Range("A" & lngTemplate_A).Value = Orders_sh.Range("B" & lngI).Value
Template_sh.Range("B" & lngTemplate_A).Value = Orders_sh.Range("A" & lngI).Value & " - " & Orders_sh.Range("E" & lngI).Value
Template_sh.Range("P" & lngTemplate_A).Value = Orders_sh.Range("D" & lngI).Value
lngSumOfItems = lngSumOfItems + 1
dblSumOfValues = dblSumOfValues + Orders_sh.Range("D" & lngI).Value
File_Name = lngSumOfItems & "(" & Orders_sh.Range("C" & lngI).Value & "-" & VBA.Round(dblSumOfValues, 0) & ").pdf"
lngI = lngI + 1
Set oCell = oCell.Offset(1, 0)
Loop Until strKey_TheName <> UCase(oCell.Offset(0, 2).Value)
Template_sh.ExportAsFixedFormat xlTypePDF, setting_Sh.Range("F4").Value & "\" & File_Name
Template_sh.Range("D1").Value = ""
Template_sh.Range("A4:P10").ClearContents
Loop Until Len(oCell.Value) = 0
Application.StatusBar = ""
MsgBox "Done"
End Sub
Hi Trying to get some help to see why this is not working in a Macro that I have setup. The area where the debugger causes an issue is at the 2nd Selection.Formula area.
Sub PrintAllonges()
'
' PrintAllonges Macro
'
' Keyboard Shortcut: Ctrl+Shift+Y
'
Dim pdfName As String, FullName As String, Path As String, lRow As Long
Set oFSO = CreateObject("Scripting.FileSystemObject")
Path = CreateObject("WScript.Shell").specialfolders("Desktop")
' Create Desktop Folder if not exists
If oFSO.FolderExists(Path & "\Allonges") Then
Else
MkDir Path & "\Allonges"
End If
'Turn off Screen Update
Sheets("MissingAllonges").Select
lRow = Cells(Rows.Count, 1).End(xlUp).Row
MsgBox (lRow)
Sheets("AllongeTemplate").Select
Application.ScreenUpdating = False
For i = 2 To lRow
Range("G6").Select
Selection.Formula = "=MissingAllonges!I" & i
Range("E11").Select
Selection.Formula = _
"=TEXT(MONTH(MissingAllonges!D" & i & "),""mmmm"")&"" ""&DAY(MissingAllonges!D" & i & ")&"", ""&YEAR(MissingAllonges!D" & i & ")"""
pdfName = Sheets("AllongeTemplate").Range("H7").Value & " - " & Sheets("AllongeTemplate").Range("G6").Value & " Allonge"
FullName = Path & "\Allonges\" & pdfName & ".pdf"
ActiveWorkbook.ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FullName, OpenAfterPublish:=False
Next i
Application.ScreenUpdating = True
End Sub
I put this in and got to work for other formulas where I am updating the loop but I can't get this to work and getting an error on syntax.
You have extra quotation marks at the end of the formula.
The corrected formula would be:
.Formula = "=TEXT(MONTH(MissingAllonges!D" & i & "),""mmmm"")&"" ""&DAY(MissingAllonges!D" & i & ")&"", ""&YEAR(MissingAllonges!D" & i & ")"
But I agree with #BigBen that the formula could be simplified, ie:
.Formula = "=TEXT(MissingAllonges!D" & i & ", ""mmmm d, yyyy"")"
My code is as follows. I just want a function to skip the email subject if it's already in the worksheet. I have already tried couple of things but didnt work. If you have follow up question please comment here. :(
If filteredItems.Count = 0 Then
Debug.Print "No emails found"
Found = False
Else
Found = True
For Each itm In filteredItems
'''
If Range("B" & Rows.Count).Value <> itm.ReceivedTime Then
Range("A" & Rows.Count).End(xlUp).Offset(1).Value = Format(itm.ReceivedTime, "yyyymmdd")
Range("C" & Rows.Count).End(xlUp).Offset(1).Value = itm.Subject
Range("B" & Rows.Count).End(xlUp).Offset(1).Value = itm.ReceivedTime
Range("D" & Rows.Count).End(xlUp).Offset(1).Value = itm.SenderName
Range("H" & Rows.Count).End(xlUp).Offset(1).Value = itm.Body
Range("H:H").WrapText = False
Range("E" & Rows.Count).End(xlUp).Offset(1).Value = "Not Started"
'''
Debug.Print itm.Subject
End If
Next
End If
'If the subject isn't found:
If Not Found Then
MsgBox "No new ticket as of" & " " & Now() & "." & " " & "Please try again later."
Else
End If
Use Worksheetfunction.Countif(Range("C:C"), "*" & itm.Subject & "*") > 0 as your check.
Also it would be best practice to reference a worksheet variable e.g.
Dim Wksht as Worksheet
Set Wksht = Activeworkbook.Sheets("Sheet1")
If Wksht.Range(...
-- this will stop your code being affected if you select another worksheet part way through.
firstly i want to write a macro for going through of every row so if valuse of item is more than 10 creat a folder base on values of that rows.in addition without a duplicate folder !
for example if there is item20 then create a folder with this name 20_NT25153_29.9 then another rows
i wanna to add this sentence ,i know my code is very simple but i am new in VBA hence need more help :)
Sub loopthrough()
With Worksheets("Output_" & Date)
fName5 = .Range("d").Value
fName1 = .Range("B").Value
fName2 = .Range("c").Value
fName4 = "_"
BrowseForFolder = CurDir()
End With
For Each cell In ActiveWorkbook.Worksheets
If cell.Range("B").Value > "10" Then
BrowseForFolder1 = BrowseForFolder & "\" & fName1 & fName2 & fName5
MkDir BrowseForFolder1
End If
Next cell
End Sub
You could use this code:
Sub Macro1()
Dim lLastRow As Long
Dim sPath As String, sNewFolder As String
sPath = CurDir()
lLastRow = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To lLastRow
If Range("B" & i).Value > 10 Then
sNewFolder = Range("B" & i).Value & "_" & Range("C" & i).Value & "_" & Range("D" & i).Value
If Dir(sPath & "\" & sNewFolder, vbDirectory) = "" Then
MkDir (sPath & "\" & sNewFolder)
End If
End If
sNewFolder = vbNullString
Next
End Sub
Fisrt of all I check for the last row index based on A column, not to loop through whole worksheet.
In a loop I've used a Dir() function with vbDirectory parameter which returns empty string when folder does not exists & in that case it creates a folder.
Is this what you're after?
Folder name is column B value _ column C value _ column D value ?
Sub loopthrough()
Dim cell As Range, fName4
BrowseForFolder = CurDir()
fName4 = "_"
With Worksheets("Output_" & Date)
For Each cell In .Range("B1", .Range("B" & Rows.Count).End(xlUp))
If cell.Value > 10 Then
BrowseForFolder1 = BrowseForFolder & "\" & cell.Value & fName4 & cell.Offset(, 1).Value & fName4 & cell.Offset(, 2).Value
MkDir BrowseForFolder1
End If
Next cell
End With
End Sub
it works for somebody need same as me
Sub Macro1()
Dim lLastRow As Long
Dim sPath As String, sNewFolder As String
sPath = CurDir()
lLastRow = workbooks(sFilename).Sheets(1).Range("A:A").SpecialCells(xlCellTypeLastCell).Row
Workbooks(sFilename).Sheets(1).Activate
For i = 2 To lLastRow
If Workbooks(sFilename).Sheets(1).Cells(i, 2).Value >= 10 Then
sNewFolder = ActiveSheet.Range("B" & i).Value & "_" & ActiveSheet.Range("C" &
i ).Value & "_" & ActiveSheet.Range("D" & i).Value
If Dir(sPath & "\" & sNewFolder, vbDirectory) = "" Then
MkDir (sPath & "\" & sNewFolder)
End If
End If
sNewFolder = vbNullString
Next
End Sub
I have this workbook with 2 sheets, the first sheet has a list of information and the second sheet is a form. I need to go through each line on the first sheet and put that information into the form, and save that sheet as a new workbook and be named from a certain cell. I basically have it all, I just need to put it in a loop and add one to the range every time it loops. Here is what I got, is there a easy way to make it loop and add one to the range. Thanks.
Sub Range_Copy()
Worksheets("Sheet1").Range("J2").Copy Worksheets("Sheet4").Range("K3:O3")
Worksheets("Sheet1").Range("K2").Copy Worksheets("Sheet4").Range("E3:H3")
Worksheets("Sheet1").Range("A2").Copy Worksheets("Sheet4").Range("A1:O1")
Worksheets("Sheet1").Range("B2").Copy Worksheets("Sheet4").Range("E29:F29")
Worksheets("Sheet1").Range("C2").Copy Worksheets("Sheet4").Range("G29:H29")
Worksheets("Sheet1").Range("D2").Copy Worksheets("Sheet4").Range("D7:O7")
Worksheets("Sheet1").Range("E2").Copy Worksheets("Sheet4").Range("L8:O8")
Worksheets("Sheet1").Range("F2").Copy Worksheets("Sheet4").Range("D8:G8")
Worksheets("Sheet1").Range("G2").Copy Worksheets("Sheet4").Range("D9:O9")
Worksheets("Sheet1").Range("H2").Copy Worksheets("Sheet4").Range("D6:O6")
Worksheets("Sheet1").Range("I2").Copy Worksheets("Sheet4").Range("A48:O48")
Application.ScreenUpdating = False
ActiveSheet.Select
ActiveSheet.Copy
ThisFile = Range("A1").Value
ActiveSheet.SaveAs Filename:="H:\Intern Work\Server List\Server Form List\" &
ThisFile & ".xlsx"
Application.ScreenUpdating = True
ActiveWorkbook.Close
End Sub
Pretty sure this is what you're looking for, however I'm not sure if you're going to hit any snags when trying to save 600 individual files -
Sub Range_Copy()
Dim i As Long, lastrow As Long
Dim sht As Worksheet, sht2 As Worksheet, newwb As Workbook
Set sht = ThisWorkbook.Worksheets("Sheet1")
Set sht2 = ThisWorkbook.Worksheets("Sheet4")
lastrow = sht.Cells(sht.Rows.Count, 1).End(xlUp).Row
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For i = 2 To lastrow
sht2.Range("A1:O1").Value = sht.Range("A" & i).Value
sht2.Range("E29:F29").Value = sht.Range("B" & i).Value
sht2.Range("G29:H29").Value = sht.Range("C" & i).Value
sht2.Range("D7:O7").Value = sht.Range("D" & i).Value
sht2.Range("L8:O8").Value = sht.Range("E" & i).Value
sht2.Range("D8:G8").Value = sht.Range("F" & i).Value
sht2.Range("D9:O9").Value = sht.Range("G" & i).Value
sht2.Range("D6:O6").Value = sht.Range("H" & i).Value
sht2.Range("A48:O48").Value = sht.Range("I" & i).Value
sht2.Range("K3:O3").Value = sht.Range("J" & i).Value
sht2.Range("E3:H3").Value = sht.Range("K" & i).Value
Set newwb = Workbooks.Add
sht2.Copy Before:=newwb.Sheets(1)
newwb.SaveAs Filename:="H:\Intern Work\Server List\Server Form List\" & sht2.Range("A1").Value & ".xlsx"
newwb.Close False
Next i
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub