How can I format selected cells (column) to 9 digit number - excel

I am trying to make a macro to change a selected column containing 7 digit numbers to 9 digits (2 leading zeros). I have no knowledge of vba. I am trying to learn but the terminology is so vast. In the meantime, I have been using code that I have found on the internet to suit my needs. For example, the code I use for the selection is as follows:
Sub SelectDn()
SelectDn Macro
Range(ActiveCell, ActiveCell.End(xlDown)).Select
End Sub
So I use this to select the data I want to format.
My colleagues at work do a lot of manual manipulation to their excel spreadsheets to create reports and I want to make the process easier for them. I know it's just a matter of right-clicking, Format cells, Custom, 000000000 but would like to do it with less steps.
Is there a way to doing this? Any help would be appreciated.
I tried this:
Selection.NumberFormat = "000000000"
Selection.TextToColumns
I used the macro recorder and formatted the 7 digit number to 9 digit. The code that it generated was:
Sub Macro14()
Selection.NumberFormat = "000000000"
End Sub
But when I try to run the macro again, I get a Compile error: Expected Function or variable and the word "Selection" in Selection.NumberFormat is highlighted. I would like the number to change from 9983743 to 009983743.

You can reference a column to some cell with EntireColumn. This is where you want to apply your format so.....
Sub Digits()
Selection.EntireColumn.NumberFormat = "000000000"
End Sub
Also, .Selection is usually not the best way to refer to a range. Is there some other, more systematic, way you can refer to your target columns? If so, I would consider revising this to do so. Maybe InputBox or pre-code the columns where you expect this format to apply.

Related

Showing a complete row using two variables in Excel

I want to ask whether it is possible to have excel print out a complete row of raw data using two variables. So like let say we have the following data:
What we wish to have is that based on the values "2018" and "A", excel should give out the complete row data automatically as done so in the yellow cells.
I know how to do it for one variable, where I have been using
Index(range,MATCH(value, range,0),column())
But I am having difficulty when there are two unique variables, based on which the row data must be extracted.
Currently, I do it in two steps. So I first filter out the year and then use the above formula to extract the row data for A or B. But it is not a very good approach and would appreciate if it can be done using a single formula.
Does anyone has any clue on how it can be done without using Pivot Table?
UPDATE
Regarding the suggestion of using VBA. Using the VBA is a good option, since then I can just use the autofilter command, but the problem is defining the cells in VBA and also how can I have one code for two different columns?
My vba code which I have used for filtering the tables is the following:
Sub Autofilter_Filter12()
Dim lo as ListObject
Dim iCol As Long
Set lo = Sheet3.Listobjects(1)
iCol = lo.ListColumns("Year").Index
with lo.Range
.Autofilter Field:=iCol, Criterial:="XXXX"
End Sub
Now the problem with the VBA code is:
it is only applied for one column and not both.
Instead of XXX, how can I define a cell into the VBA? I have tried but failed again and again.
Thank you for the help.
If range C:H is always numbers then you can use SUMPRODUCT.
=SUMPRODUCT(($A$2:$A$5=$A$7)*($B$2:$B$5=$B$7)*C2:C5)
parameter 1 parameter 2 Value to return
In C7, then select C7:H7 and press CTRL+R.
This results in this:
When this fails it will return 0.
Not very nice, but it could be partially solved with I7 =
=IFERROR(IF(SUM(C7:H7)=0,"Filter failed",""),"")
In EXCEL 365 with dynamic formula you can put multiple columns in MATCH formula by merging them with &
=MATCH(A7&B7,A1:A6&B1:B6,0)
So you can use index-match combination for your case (no matter if values are nubmers or not):
=INDEX(C$1:C$6,MATCH($A$7&$B$7,$A$1:$A$6&$B$1:$B$6,0))

VBA code to Autofill a variable number of Columns

I'm new to using VBA, I understand how to autofill a predefined number of columns/rows, however, I'd like the user to be able to input a number into a cell, and starting with a static column/row, the code autofills however many columns the user wants(in this instance how many pay periods an employee would work.) This is what I have so far.
Option Explicit
Sub AutoFill_Test()
'A1 here being where the total number of payperiods are found
Range("A1").AutoFill Destination:=Range("A2:D2"), Type:=xlFillCopy
End Sub
How would I modify this to achieve the desired result?
many thanks,

Macro to convert many columns in excel from MS to HH:MM:SS

We have a spreadsheet that's being generated by another system that provides data in Milliseconds, but I need to convert it to something that's easier to digest for the business.
I'm looking for an easy way to get it converted to duration HH:MM:SS - the columns to be converted aren't always the same columns so I'd need to be able to select certain columns and then do the conversion.
Any help is greatly appreciated - I played a bit with the macros in Excel, but it's been years since I programmed and I wasn't sure how to do some of the relative mappings for the equations.
Thanks!
Select a contiguous group of cells containing millisecond values as long integers.
Run the convertSelection sub procedure.
Select another contiguous group of cells.
Go to step 2.
Remember to put this code into a public module code sheet first.
Sub convertSelection()
With Selection
.Value = Application.Evaluate(.Address & "/86400000")
.NumberFormat = "hh:mm:ss.000"
End With
End Sub

Need help editing sumif macro to automatically update and go to next row

I have tried recording a macro that will update a sumifs formula based on two criteria, and i would like for it to move onto the next row and past the sumifs with it updating for that row.It may be helpful to note the criteria range and sum range are being pulled from a separate excel file.I cannot seem to get my code to work for more than one row. Here is my code:
Sub Sumifmacro()
'
' Sumifmacro Macro
'
'
Range("N154").Select
ActiveCell.FormulaR1C1 = _
"=SUMIFS('[CPI Project Cost Update.xlsx]PROJECT COSTING'!C24, '[CPI
Project Cost Update.xlsx]PROJECT COSTING'!C1, ""February"", '[CPI
Project Cost Update.xlsx]PROJECT COSTING'!C12, ""5395b"")"
Range("N155").Select
End Sub
Thank you in advance!
You dont need VBA for this, you could use a data table to update the formula with each new row. How were you expecting to trigger the macro ?
Type the formula manually, recorder is messing with your ranges.
When working on multiple spreadsheet, naming the ranges might come in handy.
Be aware that your values will not update if "CPI Project Cost Update.xlsx" is closed as you are using a SUMIFS.

Is there any way to speed up excel comparision in VBScript?

I first made a VBA script to compare two excel files. Then optimized it using Variant as said in this question. But then, I changed it to VBScript later. Here the method said above doesn't seem to work.
Are there any other better ways to speed up the process? Especially for large files.
My core code is as follows:-
For Each cell In objxlWorksheet1.UsedRange
If cell.Value <> objxlWorksheet2.Range(cell.Address).Value Then
'fill the color in the cell if there is a mismatch and Increment the counter
objxlWorksheet2.Range(cell.Address).Interior.ColorIndex = 3
counter=counter+1
End If
Next
It depends on what it is that you are comparing. If you have two sheets with similar tables of data it would be easier to use formulas instead of VBA code. Just create a new worksheet and enter a formula like this: =Sheet1!A1=Sheet2!A1 Then you can use Ctrl-Find to search for False
Or if you can copy the data on one sheet side-by-side, you can use conditional formatting to highlight values that are different.

Resources