Programming in VBA in Excel 2003 - excel

I'm new to the visual basics language and would like some help in writing some codes. So I'm trying to write a program that imports data from a spreadsheet and shifts current data over. So I have a spreadsheet file with 3 sheets. I would first delete the data in the third and last sheet, then cut and copy the data from the second sheet over to the third sheet and the first to the second. and then prompt the user to select a data file to import to the first sheet. How do I go about doing this ????
Thanks

You can access each cell by using
Cells(row,column)
Where the row and column are both numeric. You can set the values like this
Cell(row,column) = "This is a new value"
You can access the values like this
aString = cells(row,column)
If you want to copy data from one worksheet to another, this code will copy the first twenty six columns and rows from worksheet 2 to worksheet 3...
dim row as integer
dim column as integer
For column = 1 to 26
For row = 1 to 26
'Copy worksheet 3 value to worksheet 2's value
Worksheets(3).cells(row,column)=Worksheets(2).cells(row,column)
'Clear worksheet 2's values
Worksheets(2).cells(row,column)=""
Next
Next

You could simply delete the third sheet by executing:
Application.DisplayAlerts = False
ActiveWorkBook.Sheets(3).Delete
Then insert a new sheet and place it before Sheet 1 and 2
ActiveWrokBook.Sheets.Add Before:=ActiveWorkBook.Sheets(1)
ActiveSheet.Name = "The New Name of your newly inserted sheet"
Then fill the sheet with any data that you may want to. You did not include any details in your question about what is the source of the data, so I guess, you already know about that.

Related

how to create a macro that will NOT print a sheet when it has a specific value? [duplicate]

This question already has an answer here:
How to have a sheet NOT print when certain cells are a certain number in VBA (while loop)
(1 answer)
Closed 1 year ago.
I created a VBA script that will take information from SAP, extract it as an excel file, save it into a particular folder, and then prints out the sheets from the excel workbook that was created.
From the SAP data that is extracted, that data differentiates on the number of rows that are returned.
i.e.:
1st extraction: 5 rows are returned
2nd extraction: 15 rows returned.
3rd extraction: 10 rows returned. etc.
when these rows are extracted, they are then prepopulated into specific cells on multiple different sheets. If there are 5 rows, then that will only take up 3 sheets. 15 rows that will take up 10 sheets. However currently, the script is printing all the sheets even when they are empty.
Dim sh As Worksheet
For Each sh In ThisWorkbook.Worksheets
If sh.Name = "V" Or sh.Name = "W" Or sh.Name = "X" Or sh.Name = "Y" Or sh.Name = "Z" Then
sh.PrintOut Preview:=False, ActivePrinter:="Print&Go Americas", PrintToFile:=True, PrToFileName:=PSFileName
End If
Next sh
As you can see, it is printing sheets VWXY and Z. Is there a way to NOT print unfilled cells.
i.e.
if cells A1, A2, and A3 is 0, then i can for that sheet to NOT print.
If I read correctly, your question is "Can I modify Worksheets.PrintOut command in such a way that some cells don't get printed?".
The answer is: No, you can't. (As you can see in the official documentation, such a parameter does not exist)
However, what you can do, is create a new worksheet, copy all non-blank cells to that new sheet and print that new sheet. OR:
You can remove the blank cells from your worksheet and print the worksheet.

VBA Range_Copy Macro - How to apply same task to subsequent rows

I have a working spreadsheet with lists of items. One field could have 1 of 14 descriptions. I thought creating a macro would be quicker than manually going to a separate sheet and copying and pasting the desired description every time.
I am totally new to this, but was able to create a macro grabbing data in cell A1 in one sheet and adding that data to cell S3 in another sheet - see code below. (Each description has its own sheet with data in cell A1) My question is, how to move onto the next row and cell S4. Thanks in advance!
Sub Range_Copy()
Worksheets("Used Single Items Returns").Range("A1").Copy Worksheets("Gopher Items CSV Test").Range("S3")
End Sub
If you just want to copy into Col S on the selected row:
Sub Range_Copy()
Dim r
Set r = Selection.Cells(1).entirerow.range("S1") 'relative to row, not sheet
Worksheets("Used Single Items Returns").Range("A1").Copy r
End Sub

How to delete and add specific columns in multiple excel files

I have a file with 5 columns :
A || B || C || D || E
and I want to delete the E column and add 2 new ones with the same content in the cells. I tried to find a way in excel but it is impossible.
I used MACROS record in excel as people have suggested me to use the MACROS in my original query. But the problem is that the excel files I have, have different row numbers. So, in file one I need to add a new column with 10 rows (same text inside) and in another I need to add 2 rows.
How can I program that in the MACROS? More specifically, I want to convert this:
into this:
I only need the columns shown in picture 2. I no longer need to delete the rest. I use the files to import them on openCRM.
in a excel macro
sub ColReplaceInCurrentSheet ()
dim LastCellRow as integer
'uncomment for removing warning message
'Application.CutCopyMode = False
Columns("E:E").Delete Shift:=xlToLeft
' Define last row of data
Range("A65534").Select
Selection.End(xlUp).Select
LastCellRow = Selection.Row
for each ThisCell in Range ( "E1:F" & LastCellRow)
ThisCell.value='The Value I want here'
next
end sub
Assuming number of row is equal to the last (lower) filled cell in column A (arbitrary limited at 65534 here)
You could put any value that you want in the cell, i set it to an abritrary string here
macro is to put in a extra excel file so you can call it (file need to be open in excel) from any other excel
macro work on current active sheet.

Sheet1 of the workbook to contain name of the other sheets based on certain criteria

I've an excel worksheet which comprises of many sheet in turn let say around 5...I want to write a macro which will identify the presence of color say red in excel sheets Sheet2, Sheet3, Sheet4 and Sheet5 if there is any red color in any of the cell, it will report the sheet name in Sheet 1 of the workbook that Sheet<> so and so contains the red color in its any one of the cell.
Till yet i was only in position to identify that whether there is red color present in the particular sheet...but don't know how to report the corresponding sheet name in the sheet 1.
Sub CheckColor()
Dim Row
For Row = 2 To tsheet.UsedRange.Rows.Count
For chkcol = 1 To Sheet1.UsedRange.Columns.Count
If tsheet.Cells(Row, chkcol).Interior.ColorIndex = 3 Then
'How to report the corresponding sheet name in sheet 1???
End If
Next
Next Row
End Sub
First, I think using Range.Find() along with FindFormat would be much more efficient:
Private Sub test()
Application.FindFormat.Clear
Application.FindFormat.Interior.ColorIndex = 3
Range("A1:C3").Find(What:="", LookAt:=xlPart, SearchFormat:=True).Value = "test"
End Sub
For your principal question, I think you first need to establish whether you want to execute the code for all sheets or for a subset. If it's for all sheets, then you can simply wrap up the code above inside a for each loop on the Worksheets collection.
If you need to execute this code for a subset of the worksheets, then you can either use a rule (first 4 characters = "abcd") or hard code them in an array.
Then, it's just a matter of checking that the range returned by Find() is not nothing which means success. If the condition is met, then the current sheet is to be used by the main sheet as you want it.

Excel VBA - link rows from different sheets

Scenario: I can't post the actual workbook due to sensitive materials but I'll explain it as best as possible.
Each Sheet is divided into branches i.e.: Sheet2 is Branch 2, Sheet3 is Branch 3 and so on.
For each sheet the column are the same.
Column A has the branch number in it.
Column B has irrelevant information so i just hide that column.
Column C has a system number (specific to each account.
Intention: I want to create another sheet called CallOuts.
I want to copy some rows (from various branches) and paste them onto the 'Master sheet' (CallOuts) I can work on the CallOuts sheet instead of going to each branch. So that whenever I edit a cell it will also update/change that exact same cell in the branch sheet and vise versa with the master sheet.
Problem: I know MS Exccel has a "Paste Special" function where it adds the cell. The problem with that is it links the cell# so if I sort the Master sheet it will replace the row into the wrong branch sheet.
E.g.: If System# J112 is in branch 2 sheet, row 2 and I have the link pasted in Row 4 in the master sheet, if I make updates on the Master sheet and then re-sort it and the System# now moves to Row 2 (on the master sheet) whatever is in Row 4(on the master) will now be in Row 2, Branch 2 sheet.
I was thinking maybe a macro where I could copy and paste the entire row from the master sheet. Do some type of case selection to check which branch is in column A and then find the same system # on the branch sheet then paste the whole row.
My VBA level is novice at best so any help would be appreciated.
OK, So I saw your attempt below. I am not sure how your code works but it seems like you were on the right track. However, I don't think the Case syntax is appropriate for this situation. You simply need a macro that will read what is in your "CallOuts" sheet and use that data to update your individual branch sheets. This macro should work for you:
Sub UpdateRecordsFromMasterSheet()
Dim wksht As Worksheet
Dim wkb As Workbook
Dim row As Range
Dim Row_to_Update As Range
Dim sysnum As Variant
Set wbk = ThisWorkbook
Application.ScreenUpdating = False
For Each row In Sheets("CallOuts").UsedRange.Rows
row.EntireRow.Copy
sysnum = row.Cells(1, 3).Value
For Each wksht In ActiveWorkbook.Worksheets
For Each Row_to_Update In wksht.UsedRange.Rows
If Row_to_Update.Cells(1, 3).Value = sysnum Then
Row_to_Update.PasteSpecial
End If
Next Row_to_Update
Next wksht
Next row
Application.ScreenUpdating = True
End Sub
This assumes you have your system number in column "C". So here is what I am imagining: Each time you assign an employee a bunch of rows - your employee updates those rows - and then you paste that data back into the "CallOuts" sheet. Then you run this macro and it will update the individual branch sheets based on what is in the "CallOuts" sheet.
If your a visual person here is how it will look:
Here is the updated callouts sheet:
Here is how the branches will look before the macro:
Here is the updated branch after the macro runs:
Try it out and let me know how it works. Note: this is just a start to show you how to do something like this - you could make this process more efficient by having the macro loop through the actual workbooks that the employees submit to you so you wouldn't have to copy and paste the data into your "CallOuts" sheet every time. Good Luck.

Resources