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

Macro to aggregate data from date to date

$
0
0
I need the revenue and cost per company to be added in B3:C7 when I specify the dates I want in F3 and F4.

Thanks for the help!
Attached Files

protect cell after 1 day

$
0
0
how lock specific cells after data has been added after 1 day?
please help me out

Thanks & Regards.

Trouble creating outlook event from

$
0
0
Hey guys,

I was wondering if someone could help me get some VBA code to create a task in outlook and something to keep track which items have already been exported to excel. I only need to export the time, date and the subject. The rest is not important. It will be on a seperate worksheet. I am using office 2010. I have seen a lot of code around the web but it only shows the code and not the format and location where to enter the contents. All I want to do is create reminders for which I can define the time of the reminder and the text i want to be reminded of. And if possible a cell marked with a X or something if it already has been exported.

any help would be greatly appreciated!

and thanks in advance

Group text from a CSV file using VBA

$
0
0
I have a .csv file with an array of text which I need to combine and sort under headings.
Here is an example of one row of the csv file (data is just random to show what I want to do):
Raw CSV format:

A: Hi, my, name, is, John, B: I, am, a, builder, C: I, like, to, skateboard,

I want to group the text based on the identifiers A:, B:, C:,
So the data would look like this after running the macro:
.csv format

A: Hi my name is John, B: I am a builder, C: I like to skateboard,


Any ideas how I could create a macro for this?

Big ask but I am desperate!!

Thanks :)

I need to quickly pass data from a userform into an excel sheet.

$
0
0
I had the code working, when the userform is opened it populates data based on a user defined number. All data is related to this number in the userform and is updated from the sheet. I want to be able to update the userform data, and then pass it back to the sheet. So far I had it working with just a loop from textbox to textbox and cell to cell, however it is VERY slow. I have used arrays in the past, but it has been a long time. I need some help with how to assign the data from my userform to an array and then pass that array to a set of cells.

Code trying to pass data back to excel sheet...

Sub Update_Info()
Dim DataSheet As String
Dim Tech(10) As Variant

DataSheet = "Barrier_Data"

Set TestNo = Sheets(DataSheet).Cells.Find(UserForm2.TextBox569.Text, MatchCase:=False)
N = 1
Do
Tech(N) = Sheets(DataSheet).Cells.Find(UserForm2.Controls("Tech_" & N).Text, MatchCase:=False)
N = N + 1
Loop Until N = 10
''''''''''''''''''''''''''''''''''''''''''''''''''
r = TestNo.Row
'TechU = 1
'i = 1
'Do
'WDay = 1
c = 19
'Do
' Sheets(DataSheet).Cells(r, c).Value = TBarray(i).Value
' WDay = WDay + 1
' c = c + 1
' i = i + 1
'Loop Until WDay = 16
''Stop
'r = r + 1
'TechU = TechU + 1
'Loop Until TechU = 10
''''''''''''''''''''''''''''''''''''''
'Range(Cells(r, c), Cells(r + 16, c + 10)) = TBarray



MsgBox Tech(3)
With Sheets(DataSheet)
.Cells(TestNo.Row + 6, 5) = UserForm2.TextBox570
.Cells(TestNo.Row + 7, 5) = UserForm2.TextBox571
.Cells(TestNo.Row + 8, 5) = UserForm2.TextBox572
End With
'ActiveWorkbook.Save
'ActiveWorkbook.Close SaveChanges:=False

Unload UserForm2

End Sub

Consolidating the data of all sheets based on criteria

$
0
0
Dear All,

I want to consolidate the data of all sheets without missing any data in all sheets

Attached the excel file in which my requirement is to consolidate the data of all sheets into one sheet based on a criteria. Criteria is VB has to find the word Model in every sheet irrespective of whether the data is in the same column or same row or even it is there in the sheet more than once or even there might be blanks between the data.

ex: 56" VIVA MS MICRA,BAHRAIN sheet has model. So VB COde has to find the model in this sheet and has to consolidate in the sheet as shown in sheet1. 48" VIVA M'S MICRA,BAHRAIN sheet has model more than once. So VB code has to consolidate the data of first model in sheet1 first and the second one in the next rows in the sheet1.

This is the sample data. So I want to do it for 150 sheets right now based on your VB Code. SO please do the needful.

Do-Over When cell changes, highlight first empty row and record change

$
0
0
Re-post with updates. Apologies

I'm creating a log sheet for checking characteristics of items. I'd like to create a way to automatically record when the characteristic spec changes by highlighting the next empty row and recording the change into my last used cell in that row.

So it would do something like this;

Current spec is located in cell D7 and is 6.6"
When D7 is changed to 7.7", look for first blank cell in column A (let's say it is A16)
Highlight A16:F16 and in F16 record the change "6.6" changed to 7.7""

However D7 mentioned above is on sheet 2 and is a reference to cell A9 on sheet 1.

Please see the attached example

Thanks for any help!
Attached Files

Makro to transfer overtime

$
0
0
Hi there,

i have Problems to write a Makro for the following function:

I have created a time management for our empolyee`s and need now a makro function to transfer the over hours on a other sheet within the same workbook.

Function:
An employee can decide if he wants write over hours or not: If not they will be safed in the time management sheet.
If he wants write over hours he should push an icon and the hours will be transfered to the other worksheet.
And when they are transfered the time in the time management has to be reset to the normal starting time.

Example:

employee works from 06:00-20:00 (08:00-16:00 normal working time).
He wants to transfer over hours from the morning (06:00-08:00) ==> he push the icon and transfer the time 06:00-08:00 and date to the over hours workbook.
The Time for starting should be reseted to 08:00 according to the fact that the employee wrote over hours.

And the same should be possible for over hours after 16:00.

If anyone is interested to help me solve the Problem feel free to answer to my post and I will upload the file.

Sorry for my poor english and maybe you speak GERMAN!!

Thanks Erika

Userform that updates worksheet and reads information back

$
0
0
Hi, I think what I'm trying to do is fairly simple, but I am very new to VBA so I'm not sure what I'm doing.

I have created a userform for a fairly complex spreadsheet, and I would like the form to essentially be a more user-friendly version, with the spreadsheet just 'very hidden' out of view. So I'm hoping to have users input info on the userform, it populate the spreadsheet (which might not be the 'active' sheet), then the spreadsheet perform the calculations and auto-add some info (read-only) back to the userform. I know this is possible using VBA because I have read a few examples on the forums of people doing something similar, and I found a youtube video on it, unfortunately I do not know enough to follow the code and tailor it for my purposes! (although I will try and copy it and experiment).

I need the formatting to remain the same (some are percentages used for calculations) and restrict some of the values inputted, I have noticed with what I've done so far that if I use the 'control source' in the properties for the textbox on my userform, although it works writing information to the worksheet, it overwrites any of the formatting in the destination control source cell.

If some kind person could briefly and gently tell me how to how to do this, or point me in the right direction of a resource that does (I am keen to learn more about VBA), I would be very grateful. Thank you very much.

[SOLVED] Question about Select Case

$
0
0
Hi,

Im using this script:

Code:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H6")) Is Nothing Then
Select Case Range("H6")
      Case "Level 200":
      Range(Range("B2000").End(xlUp).Offset(1, 0), "A11").EntireRow.Delete shift:=xlUp
      Sheets("Level 200").Visible = True
      Sheets("Level 200").Select
      ActiveSheet.Range(Range("B200").End(xlUp), "H2").Copy
      Sheets("Sheet1").Select
      Range("B12").PasteSpecial xlPasteValues
      Sheets("Level 200").Visible = False
End Select
End If

End Sub

This is only Level 200 but i have some more levels in this script.
I want the script to unhide a sheet (which in this case is also called "Level 200"), copy a range and paste it back in the first sheet.
However it copies the range in the first sheet. So it copies Sheet1 Range(Range("B200").End(xlUp), "H2").Select.

Can anyone see if there is something wrong with this script?

Thanks!

[SOLVED] Adding Value to a cell which contain formula

$
0
0
Hi All,

i am need to get this query done.

my query is, i have formula in cell as
Code:

=IF($AT3>=300000,$BS3*60%,IF($AT3<=250000,$BS3*40%,$BS3*50%))
and i want to add value to that formula using VBA code like
Code:

=IF($AT3>=300000,$BS3*60%,IF($AT3<=250000,$BS3*40%,$BS3*50%)) + 500 + 600
and i try using below code , however its not getting done its showing error Type Mismatch.

Code:

Sht.Range("BT" & i).Formula + 500 + 600
can anyone please help me out with this.

thank you !

merge PDF submacro gets stuck iin the while loop unless full path of varibale defined

$
0
0
Ok so below is a code that takes pdfs from one location based on a visible cell range and then put them in a created directory and then calls another module to merge the pdfs. In the second module there is a variable strPath that when the full folder path is defined it works fine. However trying to use a structure like "..\Submittal Packaged\BOM PDF\" it gets stuck in a while loop. I have debugged and watched it step through and find every pdf file in the folder but instead of not seeing the end it loops back to the beginning.

The below code is configured in the way I am having issues.

Code:

Option Explicit ' Force variable declaration
Public Const PDF_WILDCARD = "*.pdf"
Public Const JOIN_FILENAME = "MASTER BOM.pdf"
Public Sub CopyFile2()
    ChDrive "y:"
    ChDir ThisWorkbook.Path
    MkDir ("..\Submittal Packaged\BOM PDF\")
    Dim rng As Range
    Const strNewDir As String = "..\Submittal Packaged\BOM PDF\"
   
    For Each rng In Range("L9:L1042").SpecialCells(xlCellTypeVisible)
        If CBool(rng.Hyperlinks.Count) Then
            With rng.Hyperlinks(rng.Hyperlinks.Count)
                If CBool(InStr(.Address, Chr(92))) Then
                    If Dir(strNewDir & Replace(.Address, Chr(92), vbNullString, InStrRev(.Address, Chr(92)))) = "" Then
                        FileCopy .Address, _
                        strNewDir & Replace(.Address, Chr(92), vbNullString, InStrRev(.Address, Chr(92)))
                    Else
                        FileCopy .Address, _
                        strNewDir & rng.Row & "-" & Replace(.Address, Chr(92), vbNullString, InStrRev(.Address, Chr(92)))
                    End If
                Else
                    If Dir(strNewDir & .Address) = "" Then
                        FileCopy .Address, _
                        strNewDir & .Address
                    Else
                        FileCopy .Address, _
                        strNewDir & rng.Row & "-" & .Address
                    End If
                End If
            End With
        End If
    Next rng
Call mergepdf
End Sub

Sub mergepdf()
    Dim AcroExchApp As Object, AcroExchPDDoc As Object, _
        AcroExchInsertPDDoc As Object
    Dim strFileName As String, strPath As String
    Dim iNumberOfPagesToInsert As Integer, _
        iLastPage As Integer
    Set AcroExchApp = CreateObject("AcroExch.App")
    Set AcroExchPDDoc = CreateObject("AcroExch.PDDoc")


' Set the directory / folder to use
    strPath = "..\Submittal Packaged\BOM PDF\"

' Get the first pdf file in the directory
    strFileName = Dir(strPath + PDF_WILDCARD, vbNormal)

' Open the first file in the directory
    AcroExchPDDoc.Open strPath + strFileName
   
' Get the name of the next file in the directory [if any]
    If strFileName <> "" Then
        strFileName = Dir
 
    ' Start the loop.
        Do While strFileName <> ""
   
    ' Get the total pages less one for the last page num [zerobased]
            iLastPage = AcroExchPDDoc.GetNumPages - 1
            Set AcroExchInsertPDDoc = CreateObject("AcroExch.PDDoc")
       
        ' Open the file to insert
            AcroExchInsertPDDoc.Open strPath + strFileName

        ' Get the number of pages to insert
            iNumberOfPagesToInsert = AcroExchInsertPDDoc.GetNumPages

        ' Insert the pages
        AcroExchPDDoc.InsertPages iLastPage, AcroExchInsertPDDoc, 0, iNumberOfPagesToInsert, True
   
        ' Close the document
            AcroExchInsertPDDoc.Close
   
      ' Get the name of the next file in the directory
            strFileName = Dir
            Loop
       
    ' Save the entire document as the JOIN_FILENAME using SaveFull
[0x0001 = &H1]
        AcroExchPDDoc.Save &H1, strPath + JOIN_FILENAME

End If

' Close the PDDoc
    AcroExchPDDoc.Close
       
' Close Acrobat Exchange
    AcroExchApp.Exit
End Sub

[SOLVED] Clear contents from all Rows below the last non-empty cell of a column

$
0
0
Hi guys,

I Built a small macro which allows me to autofill a certain formula (in column N) along with a dataset (from column A to M ). The problem is that, since the dataset is refreshing everyday, the number of rows of the dataset day by day is different. In case the data set is longer, the autofill formula will take care of that. In case the data set is shorter compared to the day before, there will be some formulas in excess that I don't want.

How can I cope with this ? I thought to write another macro which allows me to detect the last non-empty cell in column A and clear all the contents in the all the rows below that cell.

That said, could someone help to build this clear-contents macro ?

Thank you very much !!!

EDIT: If-Then-Else Count Down Timer

$
0
0
(Apologies !! - Hit the send button too soon - sorry)
Hi,

I have a simple (working) countdown timer in a sheet, which counts down to a specific time in seconds. Works well.

Code:

Public Sub CountdownTimer()
    Application.OnTime Now + TimeValue("00:00:01"), "CountdownTimer"
    Sheet2.Calculate
    Application.Caption = CStr(Sheet2.Range("P16").Value) & " days, " & CStr(Sheet2.Range("Q16").Value) & " hours, " & CStr(Sheet2.Range("R16").Value) & " minutes, " & CStr(Sheet2.Range("S16").Value) & " seconds"
    DoEvents
End Sub

What I would like to do, though, is when the workbook opens, the timer counts down in 1 second intervals until the actual second = 0, then switch to count down in 15 second intervals.

I have tried this, with no success ...

Code:

'Public Sub CountdownTimer()
    'If Application.Caption = CStr(Sheet2.Range("S16").Value) > ("0") Then
    'Application.OnTime Now + TimeValue("00:00:01"), "CountdownTimer"
    'Sheet2.Calculate
    'Application.Caption = CStr(Sheet2.Range("P16").Value) & " days, " & CStr(Sheet2.Range("Q16").Value) & " hours, " & CStr(Sheet2.Range("R16").Value) & " minutes, " & CStr(Sheet2.Range("S16").Value) & " seconds"
    'DoEvents
    'ElseIf Application.Caption = CStr(Sheet2.Range("S16").Value) = ("0") Then
    'Application.OnTime Now + TimeValue("00:00:15"), "CountdownTimer"
    'Sheet2.Calculate
    'Application.Caption = CStr(Sheet2.Range("P16").Value) & " days, " & CStr(Sheet2.Range("Q16").Value) & " hours, " & CStr(Sheet2.Range("R16").Value) & " minutes, " & CStr(Sheet2.Range("S16").Value) & " seconds"
    'DoEvents
   
  'End If

I know this is a simple fix for most, but I just cannot nail it down.
Any suggestions? (sample sheet attached).
Thanks.
Attached Files

to transfer specific data sheet 1 to sheet 2 by macro

$
0
0
Hi
I have one workbook in that first sheet contains some form data like student name, subject and standard I want this three rows data transfer in sheet 2 by pressing button but every new dtta will be entered and this new data would be saved in new row one by one every time like form filling

Use Cell Reference for Pivot table Filter

$
0
0
lo all,

I had previously bookmarked some VBA for this, but I can't seem to get it to work in this instance. Can someone show me what I'm doing wrong?

The cell I set to the "MrktrFieldName" is the cell I'd like to identify in the VBA to filter the "Job Rep" field of the IncomePivot.

Code:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Not Intersect(Target, Application.Range(MrktrNameField)) _
        Is Nothing Then
            UpdatePivotFieldFromRange _
            MrktrNameField, JobRep, IncomePivot
    End If
End Sub


Public Sub UpdatePivotFieldFromRange(MrktrNameField As String, JobRep As String, _
PivotTableName As String)


    Dim rng As Range
    Set rng = Application.Range(MrktrNameField)
   
    Dim pt As PivotTable
    Dim Sheet As Worksheet
    For Each Sheet In Application.ActiveWorkbook.Worksheets
        On Error Resume Next
        Set pt = Sheet.PivotTables(IncomePivot)
    Next
    If pt Is Nothing Then GoTo Ex
    On Error GoTo Ex
   
    pt.ManualUpdate = True
    Application.EnableEvents = False
    Application.ScreenUpdating = False
   
    Dim Field As PivotField
    Set Field = pt.PivotFields(JobRep)
    Field.ClearAllFilters
    Field.EnableItemSelection = False
    SelectPivotItem Field, rng.Text
    pt.RefreshTable
   
Ex:
    pt.ManualUpdate = False
    Application.EnableEvents = True
    Application.ScreenUpdating = True
   
End Sub


Public Sub SelectPivotItem(Field As PivotField, ItemName As String)
    Dim Item As PivotItem
    For Each Item In Field.PivotItems
        Item.Visible = (Item.Caption = ItemName)
    Next
End Sub

Thank ya much

[SOLVED] Autofilter by multiple cells and values

$
0
0
Hi Guys,

I have a list of 5 columns that I need to autofilter on a set piece of information.

To do this I have raised 5 autofilter instructions, which, I originally thought would work and filter one after each other until all 5 are in place.

These filters are as follows

Code:

Range("E2").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$2:$J$6747").AutoFilter Field:=5, Criteria1:="<=" & myValue, Operator:= _
        xlFilterValues
        Range("D2").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$2:$J$6747").AutoFilter Field:=4, Criteria1:="33", Operator:= _
        xlFilterValues
            Range("F2").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$2:$J$6747").AutoFilter Field:=6, Criteria1:="<>" & "A99", Operator:= _
        xlFilterValues
            Range("G2").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$2:$J$6747").AutoFilter Field:=7, Criteria1:="0", Operator:= _
        xlFilterValues
            Range("H2").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$2:$J$6747").AutoFilter Field:=8, Criteria1:="0", Operator:= _
        xlFilterValues

Each autofilter works, but the problem is the work one after each other and turn off the previous autofilter.

How do I get all 5 of these autofilters to work at once?

Checking the minimum of iterative calculations:

$
0
0
Anyone please help me.....I want to compare the results of one iteration to its previous iteration and update the lowest of the values in a new cell. The new cell values should always show the lowest value of all iterations. How could I do that?

Search For Value Specific Value in Cell, Then copy and paste row to diffrent worksheet

$
0
0
Ok, so fairly new to using Macros in Excel. I'm looking to write some code that will allow me to search through multiple worksheets for a specific value in varying columns. I then need to select, copy and paste rows that match with the criteria supplied for said columns and copy this into a different worksheet.

Apologies if this is quiet worded right but I'm hoping someone should get the gist of what I'm on about. I've had a look on several forums to find a solution to this problem but keep hitting brick walls unfortunately. If anyone needs more info to try and solve the issue let me know :)

Thanks in advance for any help!

Macro to add text to a cell in another workbook which may already contain text

$
0
0
Hi,

I'm trying to create a tool which will allow common phrases/responses which are listed in a spreadsheet to be copied to a cell in another worksheet.

I have buttons at the side of the phrases/responses and my intention was to simply press the button and this would copy the text across.

It works fine when you are using just one response, the text copies over just fine, but there are certain circumstances where I might need to use 2 or more of these common responses at the same time, and when I tried to copy/paste more than 1 I got a debug error or nothng at all happened and the only text in the cell being pasted to was the original text copied into it.

I hope that makes sense. Any advice much appreciated.
Viewing all 50040 articles
Browse latest View live