简体   繁体   English

Excel 2003 中 VBA 中的编程

[英]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.所以我有一个包含 3 张纸的电子表格文件。 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.然后提示用户将 select 一个数据文件导入到第一个工作表中。 How do I go about doing this????我该怎么做 go 关于这样做???? Thanks谢谢

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然后插入一张新工作表并将其放在工作表 1 和 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.你没有在你的问题中包含任何关于数据来源的细节,所以我想,你已经知道了。

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...如果要将数据从一个工作表复制到另一个,此代码会将前 26 列和行从工作表 2 复制到工作表 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM