nmgmarques
Board Regular
- Joined
- Mar 1, 2011
- Messages
- 133
- Office Version
- 365
- Platform
- Windows
Hi all.
Working on one of my most complicated Excel files to date. I start with the disclaimer that I know nothing of Visual Basic and all the code you see below was cobbled together by scouring the web and mostly the forums here. The idea of the script is to save the file on button press, then send a selection of the file via mail, then copy relevant data over to a index file and finally print out a copy of the file.
Here is the code. I'm pasting it all here in case anyone can use some of it in the future for whatever reason. I believe in sharing the knowledge when possible
Please don't be put off by the size of the post as it's only the last portion that is giving me grief.
Code:
<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> SaveAndSend()<br>Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br><br><SPAN style="color:#007F00">' Gravar ficheiro com nome igual ao número de ECM e indicar se é Aplicável</SPAN><br>****<SPAN style="color:#00007F">Dim</SPAN> IntNum**********<SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>****<SPAN style="color:#00007F">Dim</SPAN> NumYear******** <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>****<SPAN style="color:#00007F">Dim</SPAN> FName********** <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>****<SPAN style="color:#00007F">Dim</SPAN> FPath********** <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>****<SPAN style="color:#00007F">Dim</SPAN> NApl************<SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>****<SPAN style="color:#00007F">Dim</SPAN> sFileName****** <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>****<SPAN style="color:#00007F">Dim</SPAN> strbodySD****** <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>****<SPAN style="color:#00007F">Dim</SPAN> strbodyApl******<SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>****<SPAN style="color:#00007F">Dim</SPAN> strbodyNApl**** <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>**** <br>****FPath = "L:\10_CBR_Common\Assuntos gerais\Gestão ECM"<br>****IntNum = Sheets("ECM").Range("N4").Text<br>****NumYear = Sheets("ECM").Range("O4").Text<br>****FName = Sheets("ECM").Range("D6").Text<br>****NApl = Sheets("ECM").Range("S4").Text<br>****<br>****<SPAN style="color:#007F00">'ECM Sem Decisão para debate na reunião semanal e determinação de aplicabilidade</SPAN><br>****<SPAN style="color:#007F00">'Gravar ficheiro</SPAN><br>****<SPAN style="color:#00007F">If</SPAN> NApl = "£" <SPAN style="color:#00007F">Then</SPAN><br>****sFileName = IntNum & "_" & NumYear & "_" & FName & "_SD"<br>****ThisWorkbook.SaveAs Filename:="L:\10_CBR_Common\Assuntos gerais\Gestão ECM\Aguarda Decisão" & "\" & sFileName<br>****<SPAN style="color:#007F00">' Enviar mail</SPAN><br>****ActiveSheet.Range("A1:S7").Select<br>****<SPAN style="color:#007F00">'ActiveWorkbook.EnvelopeVisible = True</SPAN><br>****ActiveWorkbook.EnvelopeVisible = <SPAN style="color:#00007F">False</SPAN><br>****<SPAN style="color:#00007F">With</SPAN> ActiveSheet.MailEnvelope<br>********.Introduction = "Novo ECM com nº interno " & sFileName & " em " & "file://" & Replace(ActiveWorkbook.FullName, " ", "%20") & " para verificação."<br>********.Item.To = "email@domain.com"<br>********.Item.Subject = "Novo ECM para análise nº " & sFileName<br>********.Item.Send<br>********ActiveSheet.Range("A1").Select<br>****<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>****<br>********<SPAN style="color:#007F00">'ECM Com Decisão considerado aplicável</SPAN><br>********<SPAN style="color:#007F00">'Gravar ficheiro</SPAN><br>********<SPAN style="color:#00007F">ElseIf</SPAN> NApl = "R" <SPAN style="color:#00007F">Then</SPAN><br>********sFileName = IntNum & "_" & NumYear & "_" & FName<br>********ThisWorkbook.SaveAs Filename:="L:\10_CBR_Common\<SPAN style="color:#00007F">As</SPAN>suntos gerais\Gestão ECM\Aplicável" & "\" & sFileName<br>********<SPAN style="color:#007F00">'Enviar mail</SPAN><br>********ActiveSheet.Range("A1:S7").Select<br>********ActiveWorkbook.EnvelopeVisible = <SPAN style="color:#00007F">False</SPAN><br>********<SPAN style="color:#00007F">With</SPAN> ActiveSheet.MailEnvelope<br>************.Introduction = "ECM considerado APLICÁVEL com nº interno " & sFileName & " disponível em " & "file://" & Replace(ActiveWorkbook.FullName, " ", "%20") & " para dar seguimento."<br>************.Item.To = "email@domain.com"<br>************.Item.Subject = "ECM APLICÁVEL nº " & sFileName<br>************.Item.Send<br>************ActiveSheet.Range("A1").Select<br>********<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>********<br>************<SPAN style="color:#007F00">'ECM Com Decisão considerado não aplicável</SPAN><br>************<SPAN style="color:#007F00">'Gravar ficheiro</SPAN><br>************<SPAN style="color:#00007F">ElseIf</SPAN> NApl = "Q" <SPAN style="color:#00007F">Then</SPAN><br>************sFileName = IntNum & "_" & NumYear & "_" & FName & "_NA"<br>************ThisWorkbook.Save<SPAN style="color:#00007F">As</SPAN> Filename:="L:\10_CBR_Common\<SPAN style="color:#00007F">As</SPAN>suntos gerais\Gestão ECM\Não Aplicável" & "\" & sFileName<br>************<SPAN style="color:#007F00">'Enviar mail</SPAN><br>************ActiveSheet.Range("A1:S7").Select<br>************ActiveWorkbook.EnvelopeVisible = <SPAN style="color:#00007F">False</SPAN><br>************<SPAN style="color:#00007F">With</SPAN> ActiveSheet.MailEnvelope<br>****************.Introduction = "ECM considerado NÃO APLICÁVEL. Arquivado com nº interno " & sFileName & " em " & "file://" & Replace(ActiveWorkbook.FullName, " ", "%20")<br>****************.Item.To = "email@domain.com"<br>****************.Item.Subject = "ECM NÃO APLICÁVEL nº " & sFileName<br>****************.Item.Send<br>****************ActiveSheet.Range("A1").Select<br>************<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>****<br>****<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>****<br><SPAN style="color:#007F00">' Copy data to Index</SPAN><br><br><SPAN style="color:#00007F">Dim</SPAN> range1 <SPAN style="color:#00007F">As</SPAN> Range, range2 <SPAN style="color:#00007F">As</SPAN> Range, range3 <SPAN style="color:#00007F">As</SPAN> Range, range4 <SPAN style="color:#00007F">As</SPAN> Range, range5 <SPAN style="color:#00007F">As</SPAN> Range, range6 <SPAN style="color:#00007F">As</SPAN> Range, range7 As Range, range8 As Range, multipleRange As Range<br><SPAN style="color:#00007F">Set</SPAN> range1 = Sheets("ECM").Range("N4")<br><SPAN style="color:#00007F">Set</SPAN> range2 = Sheets("ECM").Range("O4")<br><SPAN style="color:#00007F">Set</SPAN> range3 = Sheets("ECM").Range("D6")<br><SPAN style="color:#00007F">Set</SPAN> range4 = Sheets("ECM").Range("G6")<br><SPAN style="color:#00007F">Set</SPAN> range5 = Sheets("ECM").Range("K6")<br><SPAN style="color:#00007F">Set</SPAN> range6 = Sheets("ECM").Range("D7")<br><SPAN style="color:#00007F">Set</SPAN> range7 = Sheets("ECM").Range("K7")<br><SPAN style="color:#00007F">Set</SPAN> range8 = Sheets("ECM").Range("Q7")<br><SPAN style="color:#00007F">Set</SPAN> multipleRange = Union(range1, range2, range3, range4, range5, range6, range7, range8)<br><br>Sheets("20160212_Acompanhamento ECM").Activate<br>****multipleRange.Select<br>****Range(Selection, Selection.End(xlDown)).Select<br>****Selection.Copy<br><br>Sheets("Indice Interno ECM").rRange("A2").Select<br>****Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _<br>****:=False, Transpose:=<SPAN style="color:#00007F">False</SPAN><br><br>ActiveWindow.SelectedSheets.PrintOut Copies:=1<br><br><br>Application.CutCopyMode = False<br>Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
My Issue is with the "Copy data to index". I'm trying to copy data from the start of the first file as per the 8 ranges specified above in the code. Please disregard the empty cells or columns. Some are merged:
I want to copy those ranges to another workbook that I am using as an index:
The idea is that this way I can open the index, manually check the last used number in column A, go back to the ECM workbook, fill out the relevant data for the next ECM, press the button and save / send / copy and paste / print the file. The copy and paste would take the ranges and paste them to the next available blank row in the index.
The present code, however, does not work. I'm attaching the 2 files via dropbox in case it helps. I'd greatly appreciate it if anyone could help me as my Googling skills have reached their end I'm afraid.
The ECM: https://www.dropbox.com/s/4f6n5rxxw2qww5m/20160212_Acompanhamento%20ECM.xlsm?dl=0
The Index: https://www.dropbox.com/s/cpmgp1eudylbsi8/Indice%20Interno%20ECM.xlsx?dl=0
Hope someone can help!
Edit: ECM file link wasn't working. Fixed.
Working on one of my most complicated Excel files to date. I start with the disclaimer that I know nothing of Visual Basic and all the code you see below was cobbled together by scouring the web and mostly the forums here. The idea of the script is to save the file on button press, then send a selection of the file via mail, then copy relevant data over to a index file and finally print out a copy of the file.
Here is the code. I'm pasting it all here in case anyone can use some of it in the future for whatever reason. I believe in sharing the knowledge when possible
Please don't be put off by the size of the post as it's only the last portion that is giving me grief.
Code:
<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> SaveAndSend()<br>Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br><br><SPAN style="color:#007F00">' Gravar ficheiro com nome igual ao número de ECM e indicar se é Aplicável</SPAN><br>****<SPAN style="color:#00007F">Dim</SPAN> IntNum**********<SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>****<SPAN style="color:#00007F">Dim</SPAN> NumYear******** <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>****<SPAN style="color:#00007F">Dim</SPAN> FName********** <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>****<SPAN style="color:#00007F">Dim</SPAN> FPath********** <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>****<SPAN style="color:#00007F">Dim</SPAN> NApl************<SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>****<SPAN style="color:#00007F">Dim</SPAN> sFileName****** <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>****<SPAN style="color:#00007F">Dim</SPAN> strbodySD****** <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>****<SPAN style="color:#00007F">Dim</SPAN> strbodyApl******<SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>****<SPAN style="color:#00007F">Dim</SPAN> strbodyNApl**** <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>**** <br>****FPath = "L:\10_CBR_Common\Assuntos gerais\Gestão ECM"<br>****IntNum = Sheets("ECM").Range("N4").Text<br>****NumYear = Sheets("ECM").Range("O4").Text<br>****FName = Sheets("ECM").Range("D6").Text<br>****NApl = Sheets("ECM").Range("S4").Text<br>****<br>****<SPAN style="color:#007F00">'ECM Sem Decisão para debate na reunião semanal e determinação de aplicabilidade</SPAN><br>****<SPAN style="color:#007F00">'Gravar ficheiro</SPAN><br>****<SPAN style="color:#00007F">If</SPAN> NApl = "£" <SPAN style="color:#00007F">Then</SPAN><br>****sFileName = IntNum & "_" & NumYear & "_" & FName & "_SD"<br>****ThisWorkbook.SaveAs Filename:="L:\10_CBR_Common\Assuntos gerais\Gestão ECM\Aguarda Decisão" & "\" & sFileName<br>****<SPAN style="color:#007F00">' Enviar mail</SPAN><br>****ActiveSheet.Range("A1:S7").Select<br>****<SPAN style="color:#007F00">'ActiveWorkbook.EnvelopeVisible = True</SPAN><br>****ActiveWorkbook.EnvelopeVisible = <SPAN style="color:#00007F">False</SPAN><br>****<SPAN style="color:#00007F">With</SPAN> ActiveSheet.MailEnvelope<br>********.Introduction = "Novo ECM com nº interno " & sFileName & " em " & "file://" & Replace(ActiveWorkbook.FullName, " ", "%20") & " para verificação."<br>********.Item.To = "email@domain.com"<br>********.Item.Subject = "Novo ECM para análise nº " & sFileName<br>********.Item.Send<br>********ActiveSheet.Range("A1").Select<br>****<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>****<br>********<SPAN style="color:#007F00">'ECM Com Decisão considerado aplicável</SPAN><br>********<SPAN style="color:#007F00">'Gravar ficheiro</SPAN><br>********<SPAN style="color:#00007F">ElseIf</SPAN> NApl = "R" <SPAN style="color:#00007F">Then</SPAN><br>********sFileName = IntNum & "_" & NumYear & "_" & FName<br>********ThisWorkbook.SaveAs Filename:="L:\10_CBR_Common\<SPAN style="color:#00007F">As</SPAN>suntos gerais\Gestão ECM\Aplicável" & "\" & sFileName<br>********<SPAN style="color:#007F00">'Enviar mail</SPAN><br>********ActiveSheet.Range("A1:S7").Select<br>********ActiveWorkbook.EnvelopeVisible = <SPAN style="color:#00007F">False</SPAN><br>********<SPAN style="color:#00007F">With</SPAN> ActiveSheet.MailEnvelope<br>************.Introduction = "ECM considerado APLICÁVEL com nº interno " & sFileName & " disponível em " & "file://" & Replace(ActiveWorkbook.FullName, " ", "%20") & " para dar seguimento."<br>************.Item.To = "email@domain.com"<br>************.Item.Subject = "ECM APLICÁVEL nº " & sFileName<br>************.Item.Send<br>************ActiveSheet.Range("A1").Select<br>********<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>********<br>************<SPAN style="color:#007F00">'ECM Com Decisão considerado não aplicável</SPAN><br>************<SPAN style="color:#007F00">'Gravar ficheiro</SPAN><br>************<SPAN style="color:#00007F">ElseIf</SPAN> NApl = "Q" <SPAN style="color:#00007F">Then</SPAN><br>************sFileName = IntNum & "_" & NumYear & "_" & FName & "_NA"<br>************ThisWorkbook.Save<SPAN style="color:#00007F">As</SPAN> Filename:="L:\10_CBR_Common\<SPAN style="color:#00007F">As</SPAN>suntos gerais\Gestão ECM\Não Aplicável" & "\" & sFileName<br>************<SPAN style="color:#007F00">'Enviar mail</SPAN><br>************ActiveSheet.Range("A1:S7").Select<br>************ActiveWorkbook.EnvelopeVisible = <SPAN style="color:#00007F">False</SPAN><br>************<SPAN style="color:#00007F">With</SPAN> ActiveSheet.MailEnvelope<br>****************.Introduction = "ECM considerado NÃO APLICÁVEL. Arquivado com nº interno " & sFileName & " em " & "file://" & Replace(ActiveWorkbook.FullName, " ", "%20")<br>****************.Item.To = "email@domain.com"<br>****************.Item.Subject = "ECM NÃO APLICÁVEL nº " & sFileName<br>****************.Item.Send<br>****************ActiveSheet.Range("A1").Select<br>************<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>****<br>****<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>****<br><SPAN style="color:#007F00">' Copy data to Index</SPAN><br><br><SPAN style="color:#00007F">Dim</SPAN> range1 <SPAN style="color:#00007F">As</SPAN> Range, range2 <SPAN style="color:#00007F">As</SPAN> Range, range3 <SPAN style="color:#00007F">As</SPAN> Range, range4 <SPAN style="color:#00007F">As</SPAN> Range, range5 <SPAN style="color:#00007F">As</SPAN> Range, range6 <SPAN style="color:#00007F">As</SPAN> Range, range7 As Range, range8 As Range, multipleRange As Range<br><SPAN style="color:#00007F">Set</SPAN> range1 = Sheets("ECM").Range("N4")<br><SPAN style="color:#00007F">Set</SPAN> range2 = Sheets("ECM").Range("O4")<br><SPAN style="color:#00007F">Set</SPAN> range3 = Sheets("ECM").Range("D6")<br><SPAN style="color:#00007F">Set</SPAN> range4 = Sheets("ECM").Range("G6")<br><SPAN style="color:#00007F">Set</SPAN> range5 = Sheets("ECM").Range("K6")<br><SPAN style="color:#00007F">Set</SPAN> range6 = Sheets("ECM").Range("D7")<br><SPAN style="color:#00007F">Set</SPAN> range7 = Sheets("ECM").Range("K7")<br><SPAN style="color:#00007F">Set</SPAN> range8 = Sheets("ECM").Range("Q7")<br><SPAN style="color:#00007F">Set</SPAN> multipleRange = Union(range1, range2, range3, range4, range5, range6, range7, range8)<br><br>Sheets("20160212_Acompanhamento ECM").Activate<br>****multipleRange.Select<br>****Range(Selection, Selection.End(xlDown)).Select<br>****Selection.Copy<br><br>Sheets("Indice Interno ECM").rRange("A2").Select<br>****Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _<br>****:=False, Transpose:=<SPAN style="color:#00007F">False</SPAN><br><br>ActiveWindow.SelectedSheets.PrintOut Copies:=1<br><br><br>Application.CutCopyMode = False<br>Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
My Issue is with the "Copy data to index". I'm trying to copy data from the start of the first file as per the 8 ranges specified above in the code. Please disregard the empty cells or columns. Some are merged:
Book1 | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | |||
1 | Ficha de Acompanhamento e Implementao de ECM | ||||||||||||||||||||
2 | |||||||||||||||||||||
3 | |||||||||||||||||||||
4 | Nmero interno: | 001 | 2016 | Aplicvel? | £ | ||||||||||||||||
5 | |||||||||||||||||||||
6 | N da ECM: | 500000011599 | Assunto: | Specification 784_0048 | |||||||||||||||||
7 | Responsvel: | Nuno Marques | Data: | 2016.02.12 | Prazo: | 2016.02.12 | |||||||||||||||
ECM |
Cell Formulas | ||
---|---|---|
Range | Formula | |
O4 | =YEAR(TODAY()) | |
G6 | =IF(S4=UPPER("Q"),"NA","") |
I want to copy those ranges to another workbook that I am using as an index:
Book1 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | |||
1 | N Interno | Ano | ECM | Aplicvel | Assunto | Responsvel | Data | Concluso | ||
2 | ||||||||||
3 | ||||||||||
Index |
The idea is that this way I can open the index, manually check the last used number in column A, go back to the ECM workbook, fill out the relevant data for the next ECM, press the button and save / send / copy and paste / print the file. The copy and paste would take the ranges and paste them to the next available blank row in the index.
The present code, however, does not work. I'm attaching the 2 files via dropbox in case it helps. I'd greatly appreciate it if anyone could help me as my Googling skills have reached their end I'm afraid.
The ECM: https://www.dropbox.com/s/4f6n5rxxw2qww5m/20160212_Acompanhamento%20ECM.xlsm?dl=0
The Index: https://www.dropbox.com/s/cpmgp1eudylbsi8/Indice%20Interno%20ECM.xlsx?dl=0
Hope someone can help!
Edit: ECM file link wasn't working. Fixed.
Last edited: