Generate Multiple page document from multiple line in excel - vba

In my excel I have multiple line like this:
Name Age Tel
John 5 12345677
Peter 10 4547567
Mary 6 46u7687867
I am wondering How can I generate multiple document from excel to multiple invoice like this:
page1:
Certification of XXXXX
John Age 5
page2:
Certification of XXXXX
Peter Age 10
page3:
Certification of XXXXX
Mary Age 6
Thanks!

Assuming the above input & comments, the following code will do the job:
Sub ListPrinting()
Dim i As Long
i = 1
For i = 2 To ThisWorkbook.Sheets(1).Range("A1").CurrentRegion.Rows.Count
ThisWorkbook.Sheets("Invoice").Range("F40").Formula = ThisWorkbook.Sheets(1).Cells(i, 1).Formula
ThisWorkbook.Sheets("Invoice").Range("I38").Formula = ThisWorkbook.Sheets(1).Cells(i, 2).Formula
ThisWorkbook.Sheets("Invoice").Range("I9").Formula = ThisWorkbook.Sheets(1).Cells(i, 3).Formula
ThisWorkbook.Sheets("Invoice").PrintOut Copies:=1, ActivePrinter:="pdfFactory Pro on FPP4:" 'Specify your printer
Next i
MsgBox i - 2 & " invoice(s) printed."
End Sub
Please note:
You should specify the name of desired printer - use macro-recorder and just select it in the list of available printers on Print dialog.
The amount of records in list is practically unlimited... well, paper and ink / toner are not))) All of them will be printed starting row 2 and up to the last non-empty string found on the list sheet.
Test the code on 1-2 items to avoid paper loss)
Sample file is shared (code tested as well): https://www.dropbox.com/s/296e200496gd7gb/ListPrinting.xlsm
P.S. Perhaps you should reformat the Date field as you desire - it uses regional settings and therefore will be displayed for you differently.

For generating multiple page document lines from multiple line in excel you should flow
some steps and for knowing that you can visit http://fetchflow.com/support/question/How-do-I-create-a-new-purchase-order.html

Related

Difficulty forcing VBA to parse dates in US/UK format

Thanks to all those who have helped thus far and helped me refine the problem
I'm receiving a CSV file from a third party software application with date time stamp in UK format that can be read by the VBA as a US date format
My machines "Region and Language Settings" are set to English (United Kingdom) but if I use
Application.LanguageSettings.LanguageID(msoLanguageIDUI)
The result is 1033 - ie: English, US and not fitting my machine settings.
CSV is a file with columns as follows:
| report name | value | % | date |
CSV created by a program and saved by users - a direct save of the CSV to disk by the third party app results in CSV table rows as follows:
"Report name","264","2.2 %","08 Jul 2021 11:05" (this imports correctly)
If users open the CSV and then save from Excel the file is saved as follows (note no quotation marks and amended date format:
Report name,1,0.00%,01/07/2021 12:37 (this fails to import correctly - cell read by VBA as 7th Jan 21)
My code should be able to handle both situations.
I'm importing the CSV data as follows:
Set csvWB = Workbooks.Open(my_FileName)
Set csvData = csvWB.Worksheets(1)
Dim csvReportAr() As Variant 'array of reprot names from column one of the CSV
Dim csvNumberAr() As Variant 'array of values (report results) from col 2 of the CSV
Dim csvDateAr() As Date 'array of the dates the reports were run
Do While csvData.Cells(cellRow, 1).Value <> Empty
Do While csvData.Cells(cellRow, 1).Value <> Empty
ReDim Preserve csvReportAr(cellRow)
ReDim Preserve csvNumberAr(cellRow)
ReDim Preserve csvDateAr(cellRow)
csvReportAr(cellRow - 1) = csvData.Cells(cellRow, 1).Value
csvNumberAr(cellRow - 1) = csvData.Cells(cellRow, 2).Value
Debug.Print "cell value = " & csvData.Cells(cellRow, 4).Value
Debug.Print "Month (of cell value) = " & Month(csvDateAr(cellRow - 1))
csvDateAr(cellRow - 1) = convertDateUKtoUS(csvData.Cells(cellRow, 4).Value)
Debug.Print "Date value = " & csvDateAr(cellRow - 1)
Debug.Print "Month of date = " & Month(csvDateAr(cellRow - 1))
cellRow = cellRow + 1
Loop
the debug statements print the following:
cell value = 07/01/2021 06:45:00
Month (of cell value) = 12
Date value = 07/01/2021
The difficulty is that the date I'm trying to import here is listed as 01/07/2021 06:45:00 in the CSV (1st July) based on the text editor view of the CSV. Just reading this cell value it seems that the VBA assumes this is a date and stores as a date saved in US format.
I suspect this is a locale issue? My locale settings seem incongruous and other users might have erroneous locale settings as well so we should be able to account for this
Questions:
Can I force VBA to read the date-time stamp as a string and then parse manually to ensure read as date in UK format? - OR - Can I read as a date time stamp specifying a specific format to read with?
Can you advise on the issue with locale with VBA stating locale is English US whilst machine settings seem to be English UK?
Can I change the locale with VBA temporarily? (other posts I've read suggests this is not advised!)
Is my use of cell.value inappropriate?
Rest of post deleted as probably no longer relevant
Thanks to all contributors so far!
I have partial but inelegant solution - I hope others can do better than me. I still can't work out why the UK date is being parsed to a US date automatically.
Thanks to all for their help as I could not have got my project to work without your helpful comments.
My partial solution:
Read the data from the CSV, assuming it may have been parsed incorrectly
If day(importDate) <=12 we are at risk of erroneous date import
create an alternate date with transposed days /months from the importDate
Use a vbYesNo MsgBox to get user to confirm which date is correct.
This works for my project as all dates in the single CSV file should be equal, You could use a similar solution of error checking a date for being read in US vs UK format if all dates in your CSV where recorded in the same format. However it will not help others who want to processes multiple CSVs or who may have a CSV with different date formats in it.
An alternative approach would be to read the csv file into an array using a CSV parser that lets you specify the file's date format, and then paste the array into a worksheet.
Here's such a parser (I'm the author):
https://github.com/PGS62/VBA-CSV
In this example, Data is pasted to a new worksheet.
Sub Demo()
Dim Data As Variant
Dim Target As Range
Data = CSVRead("c:\temp\datesukformat.csv", _
ConvertTypes:=True, _
Delimiter:=",", _
DateFormat:="D/M/Y")
Set Target = Application.Workbooks.Add.Worksheets(1).Cells(1, 1).Resize(UBound(Data, 1), UBound(Data, 2))
Target.value = Data
End Sub

Use V-Lookup to the cell number

So I have been given the task of comparing to worksheets in excel and if there is a match replacing the data from one cell with another, for example, I have 2 columns in 2 excel sheets, ID and name, I want to compare the IDs in sheet 1 with the IDs in sheet two and if it finds a match update the name linked with that ID.
Sheet 1
ID Name
1 Thomas
2 Jerry
Sheet 2
ID Name
3 Spike
1 Tom
So in the example above, I need the code to see that ID 1 is in both sheets and change the name in sheet 1 to match sheet 2, so that it looks like this:
ID Name
1 Tom
2 Jerry
I'm trying to use the Vlookup, which has allowed me to find out if it is existing or not, but then I don't know how to change the cell to match the existing one, here is my code so far:
Range("C2").Select
ActiveCell.Formula = _
"=IF(ISNA(VLOOKUP([#[ID]], 'Sheet2'!A:B,1,FALSE)), ""New"", ""Existing"")"
Do While Len(Range("A" & r).Formula) > 0
If (Range("C" & r).Value = "Existing") Then
Sheets("Sheet2").Range("A" & r & ":B" & r).Copy _
Destination:=Sheets("sheet1").Range("A" & r & ":B" & r)
Else
End If
I need to be able to get the cell number from the Vlookup so that I can use it in the if statement to pull in the correct data. There may be a simpler way to do this, if so I am open to changing everything.
Any help would be much appreciated.
I don't think there is a need of VBA for this task. As the names in "Sheet1" has to be updated by looking into the data in "Sheet2". Following formula can be used to find the updated names (by looking into data from the "Sheet2").
=IFERROR(VLOOKUP(A2,Sheet2!A2:B3,2,FALSE),B2)
IFERROR is used so as to retain the names which were not found in "Sheet2" data. Have a look at following screenshot for the example.
Easiest way is on a third sheet create 3 headings in row A: ID, Name and Source. Copy your data from sheet 1 into the first two columns and fill the third column with the text sheet 1. Do the same for sheet 2 but paste it below your sheet 1 data. You should end up with something like this:
ID Name Source
1 Thomas Sheet 1
2 Jerry Sheet 1
3 Spike Sheet 2
1 Tom Sheet 2
Then just create a pivot table from this data with source as the column, name as the row and a count of name for the data. It should look something like this:
Name Sheet 1 Sheet 2 Total
Thomas 1 0 1
Jerry 1 0 1
Spike 0 1 1
Tom 0 1 1
You can then sort on the Total column to find the duplicates which will have a total of two.
One other advantage of using this method is that it is quite easy to expand it to compare three or more lists.

Excel vba code to copy names found in multiple worksheets and count how many times they appear

I need some help writing some VBA code.
Basically I have one spreadsheet with 4 worksheets (CLIENT1, CLIENT2, CLIENT3, CLIENT4)
Each worksheet contains a column called CONTACT.
This CONTACT column contains a person name
The CONTACT column can be in a different position in each worksheet ie the CONTACT column in CLIENT1 is in Column D whereas it appears in column E in CLIENT2
I would like some help writing some code that will create a new Worksheet called SUMMARY.
The SUMMARY worksheet would contain in Column A a list of all the names found in the CONTACT column and in Column B it would count the number of times that persons name was found.
So the SUMMARY worksheet would end up looking something like this.
A B
1 John Smith 4
2 Brad Black 2
3 Gary Soth 1
4 Bob Brown 6
5 Sam Drow 2
Hope this makes sense and thanks for your help
Andy
Note : I am just playing with Excel and not using VBA but think you can achieve it without using VBA also.
You can use COUNTIF and solve your problem.
I will show you step by step. I cant attach excel here so adding images. If you wish to have excel which i prepared then just give me your email, I will send it.
This is summary which I got by using the formula (you can see that highlighted :
Client Name column can get value from the sheet where you can combine all your unique client names and use that as base.
Check Client1, 2, 3 & 4 sheets.
Hope this may help you.
Just found that in Excel you can use the Remove Duplicates function to remove all unwanted names. I can then use the count function across all 4 worksheets to get the required information.
Thanks
Andy

Combining Text and Hyperlink Into Text that is Hyperlinked

I have text in column A and URLs in column B.
I want the merged A&B shown in column C.
Example:
Column A | Column B | Column C
Product 1 | google_dot_com | Product 1 (with hyperlink to url)
Product 2 | yahoo_dot_com | Product 2 (with hyperlink to url)
Product 3 | stackoverflow_dot_com | Product 3 (with hyperlink to url)
Here is an image.
This is different from just referencing the cells and using the HYPERLINK(A1,B1) and then copying it down column C.
Here is a small test file I created that you can download if my above example wasn't clear enough. http://www.filedropper.com/stacker-help
Using Microsoft Office Professional Plus 2013, Windows 7
=HYPERLINK(B2,A2) should work as per your image requirement. Please check
store the link(column B) in text format
Use the CONCATENATE-Funktion in Excel, e.g.=CONCATENATE(A1;B1).
If you need the square bracets just add them in eg.g =CONCATENATE(A1;"[";B1;"]").
The function-names in Excel depend on your language, you might have to use a translated version (in Germany 'VERKETTEN', other languages see http://en.excel-translator.de/CONCATENATE/).
And it gets better: in german you need ";" to seperate the parameters, in english it's a ",".
Translating funktion-names like in Excel should be considered a crime.
I have found the answer I was looking for, here is the VBA code I used.
Sub MergingCells()
For i = 2 To Range("a1").End(xlDown).Row
Sheets("sheet1").Hyperlinks.Add anchor:=Cells(i, 3), Address:="http://" & Cells(i, 2).Value, TextToDisplay:=Cells(i, 1).Value
Next i
End Sub

How can I pull data from multiple files in Excel using Visual Basic?

I have an Excel problem that should hopefully be quite easy (logic-wise), but my knowledge of Visual Basic is very limited. I have a few folders with about 30 .xlsm files in them each (each file represents a day). In sheet 13 and 14 of each file are time sheets with two pieces of information that I require (sheet 13 being day shift, sheet 14 being nightshift). Column A has employee names, and column I has vehicle number. What I need to do is extract the name of the employee that was using vehicle number MT332.
For example, let's say this is one of the sheets from one of the files:
Row A: Row I:
Doe, John MT426
Smith, Jim MT856
Richard, Greg MT332
Parson, Fred MT265
I would need to extract the name 'Richard, Greg' from the sheet on the file into a list on a separate spreadsheet. The list will looks something like this:
Drivers for MT332
Days Nights
Richard, Greg Hamill, Susan
Lunz, Trent Forter, Heath
... ...
Where the Days column comes from sheet 13, and the Nights column comes from sheet 14 on each file.
Please try your best to guide me; I know VERY little of VB syntax!
Thank you!
(Note: these names aren't real)
Try this out.
Open all your workbooks that contain your driver data
Now open a new workbook, paste this code, and hit run
The code loops through all the workbooks, grabs the data, and adds it to the relevant column in your active workbook.
Sub GetMT332Drivers()
Dim wbk As Workbook, cl As Range
Range("A1") = "Drivers for MT332"
Range("A2") = "Days"
Range("B2") = "Nights"
For Each wbk In Workbooks
If wbk.Name <> ThisWorkbook.Name Then
For Each cl In wbk.Worksheets("Sheet13").Range("I2:I" & wbk.Worksheets("Sheet13").Range("I1").End(xlDown).Row)
If cl = "MT332" Then
Worksheets("Sheet1").Range("A" & Worksheets("Sheet1").Range("A1048576").End(xlUp).Row + 1) = cl.Offset(0, -8)
End If
Next cl
For Each cl In wbk.Worksheets("Sheet14").Range("I2:I" & wbk.Worksheets("Sheet14").Range("I1").End(xlDown).Row)
If cl = "MT332" Then
Worksheets("Sheet1").Range("B" & Worksheets("Sheet1").Range("B1048576").End(xlUp).Row + 1) = cl.Offset(0, -8)
End If
Next cl
End If
Next
End Sub