Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all 49888 articles
Browse latest View live

Getting run-time error 1004 application-defined or object-defined error

$
0
0
Hi friends,

The data is in columns ‘B:G’.

I want to hide entire columns from ‘I:XFD’.

I’m getting run-time error 1004: application-defined or object-defined error on the following line:

Code:

Range("G" & Range("G" & Columns.Count).End(xlLeft).Offset(2).Column, "G" & Columns.Count).EntireColumn.Hidden = True
Any help will be highly appreciated.

Thanking you in anticipation.

Macro Recording to work in an active workbook

$
0
0
I have recorded a macro that automatically creates a pivot in an active workbook but it seems lilke it recorded the actual file name of the workbook. I would like to be able to run this code in every file I open.

Sub accountlistingpivot()
'
' accountlistingpivot Macro
'

'
ActiveWorkbook.Activate
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"F03_F04_F05!R1C1:R11812C25", Version:=xlPivotTableVersion10). _
CreatePivotTable TableDestination:="Sheet1!R3C1", TableName:="PivotTable2" _
, DefaultVersion:=xlPivotTableVersion10
Sheets("Sheet1").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable2").PivotFields("State_Code")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Cycle")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
"PivotTable2").PivotFields("AccountNumber"), "Count of AccountNumber", xlCount
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
"PivotTable2").PivotFields("Billed_OB"), "Sum of Billed_OB", xlSum
With ActiveSheet.PivotTables("PivotTable2").DataPivotField
.Orientation = xlColumnField
.Position = 2
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Sum of Billed_OB")
.NumberFormat = "#,##0"
End With
Sheets("Sheet1").Select
Sheets("Sheet1").Name = "Summary"
End Sub

Thanks

Property to indicate macro is running

$
0
0
Is there any inbuilt property that indicates whether a macro is currently being run?

I looked into Application.Interactive but this still remains true while the macro runs.

Adding a validation formula when it evaluates to zero

$
0
0
This code successfully updates the validation formula in the desired cells when a valid value exists in the prescribed MATCH cell above. However, it errors out it errors out at the .Add Type line when it reaches a cell for which the MATCH cell above is blank or contains a value that causes the validation formula to evaluate to an error. I need the VBA code to apply the validation formula to the cell even when the source formula evaluates to an error (i.e. when the MATCH cells are blank or contain a value other than a valid “SectionType”.)

I’ve attached a test worksheet to illustrate the problem. Any assistance is much appreciated!

Code:

Sub ValidationUpdate()
    Dim r As Long, mySheet As Worksheet

    ' Modify the validation formula that exists across multiple cells in column "F" within the worksheet.
    ' The cell containing the lookup value in each formula is always two cells above
    '      the cell that contains the validation list formula.

    Set mySheet = ThisWorkbook.Worksheets("Section Updates")
   
    For r = 22 To 37 Step 5
            With mySheet.Range("F" & r).Validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                    xlBetween, Formula1:="=CHOOSE(MATCH($F$" & r - 2 & ",SectionTypes,0),TypeOne,TypeTwo,TypeThree)"
                .IgnoreBlank = True
                .InCellDropdown = True
                .InputTitle = ""
                .ErrorTitle = ""
                .InputMessage = ""
                .ErrorMessage = "Please select response from the drop-down list provided"
                .ShowInput = True
                .ShowError = True
            End With
    Next r
End Sub

Why does Excel crash when selecting code, possibly vba issues.

$
0
0
Firstly I have no VBA experience. Reading through the forums, I believe it was requested to label everything within the code. But it's hard to label everything, because I'm not really sure about all the commands. Some surely being unnecessary, some left just incase I add other options to the userform.

I created this file from scratch, after reading countless forums, watching tons of videos, and modifying a lot of code. It really does what I want it to, but I know there is possibly a better way.

My current issue is randomly the file will crash, I could be just changing code, just using the userform, even just saving the file. I'm never doing anything particular when the file crashes. But once the file has crashed, it will no longer allow me to modify the code without it crashing again. If I try to put my cursor within the code for the userform to try and figure out why or where, excel will crash again. From this point I have to reopen another file, copy the data to a template sheet, and start over. Which will work for an hour or two, then crash again. It makes it hard to troubleshoot this issue, especially with my VBA experience (none).

I was wondering if anyone could find an error in the code that would cause this, maybe something I could do better. I can not recreate the issue at home, this only happens with my computer at work. But the issue persist with multiple workstations. And why it would cause excel to crash just by selecting the code. This is not one of the crashed saves, I have those at work if someone wants to try it. When I send the corrupted file home, it reacts the same way. Unable to modify the code, can't place my cursor within the code lines.

The only thing I change when I get it to work is the file location, which is located on a network.

I've tried restarting the machine, and not enabling macros, but same behavior.

Thanks to anyone who checks it out. Coaching Action Form (Template).xlsm

[SOLVED] Linking -- a faster way?

$
0
0
I want to link my "1-31" sheets to my averages sheets. However, I realized this is gonna take a long time.
Is there a faster way I could do this?Nalco Water Chemistry.xlsx

[SOLVED] Sort individual column Alphabetically

$
0
0
Hello:

Please refer to attached file.
I have lots of data in column B thru Column E
I need an easy way either by VB Code or other mean to sort alphabetically the selected column form dropdown list in cell H2
Let me know if you have any questions.
Thanks.


Riz
Attached Files

Highlight Textbox Text during a loop

$
0
0
This is driving me crazy...

I found a lot of information online, but it is confusing.

I have a userform that loops through data and enters the current value as textbox1.text. I want the textbox1.text to be highlighted automatically so the user doesn't have to highlight it all if they want to change the value. I can get the highlight to work only on the first iteration (iCounter = 1), when iCounter > 1 the text will not highlight.

I don't need to check the .text, but I think I need a Cancel = True in there somewhere...just not sure where...or if I am using the correct event. The .setfocus automatically moves to the CommandButton...this is why I think the highlight will not occur.

All I want to do is have the text in the textbox highlight every time iCounter increases. Any help would be appreciated.

Code:

Private iCounter As Integer

Private Sub UserForm_Initialize()
    iCounter = 1
    TextBox1.Text = CStr(iCounter)
End Sub

Code:

Private Sub TextBox1_Enter()
    'This works only the first time
    TextBox1.SetFocus
    TextBox1.SelStart = 0
    TextBox1.SelLength = Len(TextBox1.Text)
End Sub

Code:

Private Sub CommandButton1_Click()
    If iCounter < 3 Then
        iCounter = iCounter + 1
    Else
        Unload Me
    End If
    TextBox1.Text = CStr(iCounter)
End Sub

Capture.JPG

Help With Compare Two Excel Sheets And Merge

$
0
0
Hello,


I have two sheets (sheet1, sheet2), and column "A" being the master identifier for both sheets. I need to compare both sheets, and what ever is common between both in column "A", I need to copy the what is in the column "D" cell in sheet1 to the corresponding "D" cell in sheet2.

Thank in advance with any help.


Thank you,
DB

Function in two or more places

$
0
0
Hello,
I have a function which is working for one place in the Sub code:

Code:


Public Function Counter(ByVal ctl As Control, s As Double, rng As Range) As Double
Dim size As Double
With UserForm1.Frame1


   
      size = Application.WorksheetFunction.RoundUp(Len(ctl) / 52, 0)
   
       
       
    With rng
        .RowHeight = (size * 10)
        Counter = .RowHeight
       
    End With

End With
   
End Function

But When I call it in more than one place, I get an error <ByRef arugment type mismatch>

I call the function like:

Code:

With rngMotivation
            .Offset(0, 1).Value = "• " & txtMotivation.Text
           
            size1 = Counter(UserForm1.txtMotivation, 0, rngMotivation)
            .RowHeight = size1
           
           
        End With

It highlights the third parameter - rngMotivation


Thanks!

Macro to rename cell content based on the starting character until found same character

$
0
0
Hi , Is there a macro to to rename cell content based on the starting character until found same character .
B1 Cafe
B2 qty
B3 unit
B4 Cafe
B5 Lobby
B6 qty
B7 unit
B8 Lobby

It will Be
B1 Cafe
B2 Cafe
B3 Cafe
B4 Cafe
B5 Lobby
B6 Lobby
B7 Lobby
B8 Lobby
Attached Files

Macro GUI to input data to certain cell

$
0
0
Hi, Is there a macro to input data to certain cell using GUI.

For example when I press ctrl-X there will pop up windows GUI asking Location =1.Cafe 2. Riva, then another windows GUI appear asking Type = 1.Duty 2.Entertaint , then another windows GUI appear asking Type = 1.food 2.beverage, and so on.

I attached the GUI flow in excel file,thx
Attached Files

change cell upon cell

$
0
0
dears ,, as usually excpeted form you , i need a support in a small thing i think .

1) is there an option to disable a cell ? make certain cells in grey color as disabled ?

what i want , i have two selection drop down menus ( both are on data validation ) i want if first dropdown is selected ( a text from it's options ) disable other cells , so if there is a way to disable ( not lock ) other cells .

Large Data base, need to sift through and find duplicates to find x.

$
0
0
Hey guys,


Ill just get right to the point. I have a database of about 400,000 lines. My goal is to fill in all the commodity codes. The rows in the above picture with N/A can be filled in with the same commodity code since they both share the same description.

My question is, how can I sort or build a table that will only show me the following:
-contains the same item description
-has 1 commodity code filled out
-has 1 N/A
I want to be able to see only rows with those conditions so that I am not sifting through the others.

I really don’t want to have to sift through the entire database and I am just not seeing the answer.

thanks,

Tyler G
Attached Images

Data entered by barcode scanner distributed automatically to other Sheets in same workbook

$
0
0
Hello
Have a little problem hope you guys can help. Posted this in General but was advised to move it here.

Sheet 1: Data entered into column A only, consecutively by a barcode scanner.

Ex;
column A
Bob12
Jon13
Tom12
Jack13

Sheet 2: I need names with #12 at the end to be copied and sorted by columns into sheet 2 with date stamps next to them.
Ex:
column A column B
Bob12-9/12 Tom12-9/12

So if 2 days later, data is entered again with Bob12, it will appear below Bob12-9/12 as Bob12-9/14.
Also, if i were to delete data on sheet 1 monthly, sheet 2's data would still be there.

Been searching a lot but no success. Tried with Vlookup but data is dynamic, hence I must post. Thanks a billion if some one can help.

Run-time error '6' overflow

$
0
0
please how can i tackle this problem in my excel file. i need someone to help me fix it...... Please.

Adding Text to a Shape with VBA

$
0
0
I have a Shape (rounded rectangle) that I want to add text, then edit the text via VBA. When I record a macro doing this, the closest I can come is code that copies new text from a cell and then selects a cell (probably where the upper left corner of the shape is located), followed by ActiveSheet.Paste. But this is too inaccurate. I want to name the shape and then control the text.

Hide columns basic on user selection a week

$
0
0
I need a way in a user form for the end user to select a 1 week time span. The one week time span to be able to start on either Sunday or Wednesday (depending on user form). I do currently have a working copy on the workbook that for each of the buttons it is code to hide everything but the week selected and every year I have to go and change the macros to show the proper information, but I know there has to be a better way of doing this. I have attached a sample sheet.

sample3.zip

The User Forms are
Pick_Week_D
Pick_Week_M

Any Help you can provide would be much appreciated.

Thank You

Current Code
Code:

' This Overtime Sheet is brought to you by Bramz
' Each button On this User Form Will hide the dates that are not
' associate with the week selected for day shift OT
' Then loads the Typ_OT User Form
Private Sub btn_w_1_Click()
    Range("J:NG").EntireColumn.Hidden = True
    Unload Pick_Week_D
    Typ_OT.Show
End Sub
Private Sub btn_w_2_Click()
    Range("G:I,Q:NG").EntireColumn.Hidden = True
    Unload Pick_Week_D
    Typ_OT.Show
End Sub
Private Sub btn_w_3_Click()
    Range("G:P,X:NG").EntireColumn.Hidden = True
    Unload Pick_Week_D
    Typ_OT.Show
End Sub

Find not working with numbers

$
0
0
Hi,
I have a script that looks like this:
Code:

                    Set rngGraph1 = .Find(What:="YY", _
                                              After:=.Cells(1, 1), _
                                              LookIn:=xlValues, _
                                              LookAt:=xlWhole, _
                                              SearchOrder:=xlByRows, _
                                              SearchDirection:=xlNext, _
                                              MatchCase:=True)
                    Set rngGraph2 = .Find(What:="99", _
                                              After:=.Cells(.Cells.Count), _
                                              LookIn:=xlValues, _
                                              LookAt:=xlWhole, _
                                              SearchOrder:=xlByRows, _
                                              SearchDirection:=xlNext, _
                                              MatchCase:=True)

The first rngGraph1 returns the address but the second rngGraph2 does not work.
I figured out why and that was because of What:= being a number instead of text
However, in my real code, I don't have it set to "YY" or "99" it is actually What:=Mid(arr100(i), 1, 2) and What:=Mid(arr100(i), 4, 2) respectively.

How do I fix this? Please help.

Macro to mirror(copy) present sheet to new

$
0
0
I want to copy this spread sheet to a new sheet by clicking on a button or new tab using a Macro. Is this possible or is it asking for a lot or impossible to do?

Thanks
Attached Files
Viewing all 49888 articles
Browse latest View live