How to delete and add specific columns in multiple excel files - excel

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.

Related

How to reassign individual rows among multiple worksheets based on multiple cell values

I have four table-formatted worksheets (A, B, C, and D) each with rows of different data beneath matching column headers. I'm trying to write a VBA macro to cut rows from one sheet and paste them into the bottom of another based on the value of a single cell in a given column. I know this has been done from one individual sheet to another and I found the basic code to do so. However, I wanted to modify that to apply across all the worksheets, account for multiple permutations of the same value, and avoid re-checking rows that are already/have been placed in their "proper" worksheet, i.e., all rows marked 'Archived' in all its various permutations in column 17 of their respective other worksheets get shuffled over to the 'D' worksheet once the word archived is typed into that cell and the enter key is pressed. I think I'm screwing up something basic here with the "<>" and "or" portions, but not sure how to fix them. I'm also not sure if it is better to link this command to the 'Enter' keystroke or just have it check the values automatically?
Sub MoveRows()
Dim k As String
a = Worksheets(k).Cells(Rows.Count,1).End(xlUp).Row
If Worksheet.Name <> "D" Then
For i = 2 To a Step -1
If Worksheets(k).Cell(i,17).Value = "Archived" or "archived" or "ARCHIVED" Then
Worksheets(k).Rows(i).Cut
Worksheets("D").Range("A1").End(xldown).Offset(1,0).Insert
End If
next i
Else 'do nothing'
End Sub

How do I copy the rows of which certain cells contain text, and paste them to another location?

Some cells in column J contain texts and the others are empty.
I want to 1. find all the rows where column J contains text 2. copy these rows 3.paste them somewhere else (in the same order as the initial table)
Sub org1()
Dim a As Range
For Each a In Range("j2:j500")
If Not a.Value = "" Then
a.EntireRow.Copy
a.Offset(100, 0).Insert
End If
Next a
End Sub
error
There is no need to do a structure like that. Simply filter the column excluding the blank cells, then copy the filtered column to wherever you want. If you don't know how to do the filter in vba, record a macro and do the process.
If you don't want the column filtered after that, just remove any filters afterwards with Sheets("YourSheet").AutoFilterMode = False

Excel VBA - I need to paste a range of data down to the end the data of rows.

ie: I have data in E2:W2 and I'd like to paste it to E:W depending on how many rows there are in column A. Each time I run the macro it will be a different number of lines. It could be 3 or 2000 rows
Also, could be 1, so no copying necessary.
Something like this will copy E2:W2 all the way down until there are no filled cells in column A
Sub CopyEW
if cells(rows.count,1).end(xlup).row>2 then _
range("E2:W2").copy range("E2:W2").resize(cells(rows.count,1).end(xlup).row)
end if
end sub

Programming in VBA in Excel 2003

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.

Checking if cell is populated

I have an HTML report that can be opened in Excel. I have written macros to format this report so it is sortable and can have filters applied.
I am trying to grab text between slashes. The report size, how many rows, is unknown.
Column A is blank. Column B will contain the full path name. IF column B is populated, column C should have the text between the slashes.
For example, column B contains R:\testdocuments\test.html. I want column C to contain testdocuments.
Code I have for column C:
Range("C9").Select
ActiveCell.FormulaR1C1 = _
"=MID(RC[-1], FIND(""\"",RC[-1])+1, FIND(""\"",RC[-1], FIND(""\"",RC[-1])+1)-FIND(""\"",RC[-1])-1)"
Range("C9").Select
Selection.AutoFill Destination:=Range("C9:C65000"), Type:=xlFillDefault
Range("C9:C20000").Select
This is not effective because it takes forever to sort/filter anything.
How can I check if column B is populated and if it is, to populate C with the text between slashes for the ability to filter by directory?
First of all, a few general tips:
When writing macros, you can work with Ranges without selecting them.
Use the following code at the beginning of your macro to prevent the application from animating (which slows it down): Application.ScreenUpdating = False
Edit: Make sure and set it back to 'True' after you're done.
Instead of Range("C9:C65000"), you can actually let Excel find the last row for you using Range("A1").SpecialCells(xlCellTypeLastCell).Row.
So, the macro might look something like this:
Sub FillFormulas()
Dim row As Integer
Application.ScreenUpdating = False
row = Range("B9").SpecialCells(xlCellTypeLastCell).row
Range("C9").Formula = "=MID(B9, FIND(""\"",B9)+1, FIND(""\"",B9, FIND(""\"",B9)+1)-FIND(""\"",B9)-1)"
Range(Range("C9"), Range("C" & row)).FillDown
End Sub

Resources