vba 如何从不同电子表格的单元格中获取某些字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22488825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:59:26  来源:igfitidea点击:

How can I take a certain characters from a cell of a different spreadsheet

excelvbaspreadsheet

提问by user3434646

I have a problem with my current code from Excel VBA.

我当前的 Excel VBA 代码有问题。

I need to take only the last four digits from another spreadsheet, but it doesn't seem to be possible.

我只需要从另一个电子表格中提取最后四位数字,但这似乎不可能。

I'm currently using the Sheets("Sheet2").Cells(y, cOTr).Valueinstruction to take values of other spreadsheets and the LEFT(a,2)and RIGHT(a,2)instructions to take only specific characters of a string data.

我目前使用的Sheets("Sheet2").Cells(y, cOTr).Value指令采取其他电子表格的价值观和LEFT(a,2)RIGHT(a,2)指示采取字符串数据只有特定的字符。

I just can't find a way to combine those two instructions.

我只是找不到结合这两个指令的方法。

E.g. I want to copy only the first 4 numbers of Cells from the second column from sheet 2, which has the numbers similar to 6657-2 in it, to sheet 1.

例如,我只想将工作表 2 的第二列中的前 4 个单元格编号复制到工作表 1,其中的编号类似于 6657-2。

Sorry if my explanation wasn't clear enough.

对不起,如果我的解释不够清楚。

Edit: Lets's say I want to make a comparison between a table from spreadsheet 1 and a table from spreadsheet 2: the first table has values with four digits (e.g. 3333) and the second has values with 4 digits, a hyphen and another digit (e.g. 2222-3). I need to make a comparison between table 1 and 2, which are from different spreadsheets and to do that I only need the first four digits from table 2. I already have the logic for the program and it's running perfectly with some tests I did with only 4 digit numbers, I only need a way to take those first four numbers, something like using the "left(cells(1,2),2)" instruction but with cells of another spreadsheet.

编辑:假设我想比较电子表格 1 中的表格和电子表格 2 中的表格:第一个表格具有四位数字(例如 3333)的值,第二个表格具有四位数字、一个连字符和另一个数字(例如 2222-3)。我需要对来自不同电子表格的表 1 和表 2 进行比较,为此我只需要表 2 中的前四位数字。我已经有了程序的逻辑,并且它在我做过的一些测试中运行良好只有 4 位数字,我只需要一种方法来获取前四个数字,例如使用“left(cells(1,2),2)”指令但使用另一个电子表格的单元格。

回答by Loopo

You can combine text / strings by using the ampersand operator &

您可以使用与号运算符组合文本/字符串 &

e.g.

例如

=LEFT(A2,2) & RIGHT(A2,2)

Alternatively, you can also use CONCATENATE

或者,您也可以使用 CONCATENATE

e.g.

例如

=CONCATENATE(LEFT(A2,2),RIGHT(A2,2),"some other string","and so on")

回答by Sanjeev Ammanna

you can us this formula: =LEFT(Sheet2!A1,4).

您能给我们这个公式:=LEFT(Sheet2!A1,4)

enter image description here

在此处输入图片说明

Sheet2 A1 is where you have data 2222-3.

Sheet2 A1 是您拥有数据 2222-3 的地方。